2 ms·
JavaScript actually has a pretty ergonomic way of doing currying natively: const add = (a) => (b) => a + b; [1, 2, 3, 4].map(add(2)); // [3, 4, 5, 6]
by brundolf 6y ago
JavaScript actually has a pretty ergonomic way of doing currying natively:
const add = (a) => (b) => a + b;
[1, 2, 3, 4].map(add(2)); // [3, 4, 5, 6]
- javajosh 6y agoI like this too (and use a version myself) however this form only supports one argument at a time. E.g. add(1,2) is going to return the curry of add(1), which is not what you want. The OP's function doesn't have that problem. Something I would really like, although this would require some language-level changes, I think, is the ability to curry arguments in any order, maybe using placeholders. So you could say add(_, 2) and get back a function of one argument, such that if you call it with 1, for example, you invoke add(1, 2).
- brundolf 6y agoWhat it ends up being for normal usage is something like this: add(1)(2) Which is admittedly a little bit clunky, though from a quick read I don't think the library helps with this. You might be interested in the "pipeline operator" proposal. In addition to pairing nicely with curried functions, it offers a certain amount of re-ordering.
- javajosh 6y agoYeah I think your quick read is off: add4(1)(3)(5)(10) add4(1)(3,5)(10) add4(1,3,5,10) // 19