Reputation: 157
I tried to set a condition statement using R.cond. Firstly, i got the array size according to the input (i.e. [1,2,3]), then check if the array size is greater / equal to the input size (i.e. 3). But i got an error message. I would like to know why the error occur and how to fix it, thanks.
R.cond([
[R.compose(R.gte, R.length), () => {console.log(1)}],
[R.T, () => {console.log(2)}]
])([1,2,3])(3)
Error message: R.cond(...)(...) is not a function
Upvotes: 2
Views: 2827
Reputation: 50807
I think there are two separate issues here. First of all, because the vast majority of calls to cond
-functions are unary, it does not curry the result.
So you cannot call it as (...)([1,2,3])(3)
. You will need to do (...)([1,2,3], 3)
.
But that won't fix the other issue.
compose
(and its twin pipe
) do take multiple arguments for their first call, but after that only a single argument is passed between them. So the only value being passed to gte
is the result of length
.
You can fix this in several ways. Perhaps the simplest is:
const fn = R.cond([
[(list, len) => list.length >= len, always(1)],
[R.T, always(2)]
]);
fn([1, 2], 3); //=> 2
fn([1, 2, 3], 3); //=> 1
(Note that I changed your console.log
to a function that returns a value.)
If you want to make this points-free, you could switch to use Ramda's useWith
like this:
const fn = R.cond([
[R.useWith(R.gte, [R.length, R.identity]), always(1)],
[R.T, always(2)]
]);
But as often happens, I think the introduction of arrow functions makes tools like useWith
less helpful. I find the earlier version more readable.
You can see these in action on the Ramda REPL.
Upvotes: 2