nielswijers
nielswijers

Reputation: 44

How to curry multiple parameters with ramda without specifically invoking the return function

is it possible to create af function with multiple arguments that returns a compose and invoke it automatically with the last argument. see the example below

const data = {
  hello: 'world'
}

const propContains = (values, key) => compose(contains(__,values),prop(key))
propContains(['wor','ld'], 'hello')(data) // --> false
propContains(['wor','ld'], 'hello', data) // --> Function

// this is working but don't want to invoke the compose with the last argument
const propContains2 = (values, key, obj) => compose(contains(__,values),prop(key))(obj)
propContains2(['wor','ld'], 'hello', data) // --> false  

Here is the Ramda REPL

Upvotes: 0

Views: 1298

Answers (1)

Scott Sauyet
Scott Sauyet

Reputation: 50807

It's fairly unclear what you're looking to do. Here's one version that looks for only a single value:

let propContains = curry((value, key, obj) => 
    contains(value, prop(key, obj)));
propContains('wor', 'hello', data); //=> true
propContains('ld', 'hello', data); //=> true
propContains('foo', 'hello', data); //=> false

And here's one that checks multiple values, essentially mapping over the above:

let propContains = curry((value, key, obj) => 
    map(contains(__, prop(key, obj)), value));
propContains(['wor', 'ld', 'foo'], 'hello', data); //=> [true, true, false]

And if you want to see if they're all contained, you can do

let propContainsAll = curry((value, key, obj) => 
    all(contains(__, prop(key, obj)), value));
propContainsAll(['wor', 'ld'], 'hello', data); //=> true
propContainsAll(['wor', 'ld', 'foo'], 'hello', data); //=> false

You could do a similar thing for any.

As all of these are curried with Ramda's curry, you can choose whether to supply the final parameter on the first call to get the data or to skip it and get back a function. It's not clear to me if this is what you're looking for, though.

Update

I was quite confused by the name. "propContains", which to me meant that the value of the property contained something. Now I see that you really want something I might write as "propContainedIn" or some such:

// propContainedIn :: [a] -> String -> Object{String: a} -> Bool
const propContainedIn = curry((values, key, obj) => 
    contains(prop(key, obj), values));
propContainedIn(['wor', 'ld'], 'hello')(data) //=> false
propContainedIn(['wor', 'ld'], 'hello', data) //=> false
propContainedIn(['wor', 'ld', 'world'], 'hello', data) //=> true
propContainedIn(['wor', 'ld', 'world'], 'hello')(data) //=> true

Although I'm sure that with enough work this could be made points-free, it would surely be much less readable than this, so I don't see the point.

You can see this too on the (updated) Ramda REPL.

Upvotes: 1

Related Questions