Ramda: curry not working when being partially applied in point free style

Created on 5 Jul 2018  路  3Comments  路  Source: ramda/ramda

image
this returns => [ 1, 2, 3 ]

image
while this returns => [ function, function, function ]

Why didn't it work in point free style? What's the difference between these two?

Most helpful comment

Your second example is the equivalent of:

console.log([1, 2, 3].map(R.unary(b)))

The reason it does not work is your function wants two arguments, and if given just one, it returns a function. That's how currying normally works.

Your first example works (not sure if it's "as expected", though), because Array.prototype.map() will pass 3 arguments, not just one, second argument being the index.

All 3 comments

Your second example is the equivalent of:

console.log([1, 2, 3].map(R.unary(b)))

The reason it does not work is your function wants two arguments, and if given just one, it returns a function. That's how currying normally works.

Your first example works (not sure if it's "as expected", though), because Array.prototype.map() will pass 3 arguments, not just one, second argument being the index.

@kanitsharma:

You may be running into the issue that Ramda's curry is not a standard curry. The idea is that if

const f = (a, b, c) => /* ... */
const g = curry(f)

then, while g still has an arity of 3, you can use it in any of these manners:

g(a, b, c) ~> f(a, b, c)
g(a, b)(c) ~> f(a, b, c)
g(a)(b, c) ~> f(a, b, c)
g(a)(b)(c) ~> f(a, b, c)

Note that your first example should log [1, 3, 5], not [1, 2, 3]. As @foxbunny pointed out, you're passing the index when you use Array.prototype.map. So you get calls to b(1, 0), b(2, 1), and b(3, 2), returning [1 + 0, 2 + 1, 3 + 2], which is [1, 3, 5]. (Actually, third parameter is supplied as well: the entire list. But it's ignored in this function.)

Note also that [1, 2, 3].map(b(20)) does return [21, 22, 23]

When you make the function unary, whether with a unary decorator or via x => b(x), then you get something equivalent to [x => 1 + x, x => 2 + x, x => 3 + x]

It's not clear to me what you're looking for as a correct result. You should expect when you map your function b over a list of numbers to get a list of functions. But that's only with a map that doesn't pass the additional parameters. For instance

R.map([1, 2, 3], b) ~> [x => 1 + x, x => 2 + x, x => 3 + x]

Also, a quick note. Please don't paste pictures of code, but the actual code itself. (See the Examples section in GitHub's Markdown Guide if you have any questions.)

@foxbunny @CrossEye Thanks for your explanations, I forgot about the additional parameters in Array.prototype.map and yeah @CrossEye It was logging [1, 3, 5], my bad.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

woss picture woss  路  3Comments

Luxcium picture Luxcium  路  3Comments

ldk18501 picture ldk18501  路  3Comments

ashaffer picture ashaffer  路  4Comments

DanielTimLee picture DanielTimLee  路  3Comments