Currying is a process in functional programming in which we can transform a function with multiple arguments into a sequence of nesting functions. ...
For further actions, you may consider blocking this person and/or reporting abuse
Nice article, but you seem to have a somewhat wrong idea of what currying is, and are conflating it with partial application. To clarify:
Partial application
A function is called with less arguments than it needs, and returns a new function that takes the remaining arguments. Some languages support this out of the box:
And some, like JS, can only approximate this behaviour using higher-order functions:
Currying
On the other hand, currying is the process of taking any function and converting it into a series of nested functions of arity 1:
This can sometimes be done automatically, but only in languages that offer some mechanism to inspect a functions arity, and generate a new (nested) function until the right number of arguments has been provided.
Just for fun, here's an automatic currying function:
For your above definition of currying how can you say just one arity ? the last function can have any arity. Also, bellow matches to your definition of partial as well.
const add = a => (b, c) => a + b + c
add(1)(2,3)
The practical answer is that currying makes creating anonymous functions much easier. Even with a minimal lambda syntax, it's something of a win; compare:
map (add 1) [1..10]
map (\ x -> add 1 x) [1..10]
If you have an ugly lambda syntax, it's even worse. (I'm looking at you, JavaScript, Scheme and Python.)
net-informations.com/js/iq/default...
I honestly don't think JS has such a bad lambda syntax;
[1,2,3].map(x => 1+x)
is still quite acceptable compared to what we have in Lua:function(x) return 1+x end
** keep in mind that Lua is intentionally minimalistic, making it an easy transpilation target for languages with more convenient syntax, so this is effectively not a big problem
Just to be honest, I got scared of things like that:
const mul = x => y => x*y
This is something readable, but some people can enjoy this "non-hability" of creating variable names, that is the worse thing you can do, but the action of refactoring to become faster, I totally agree, but needs to be well written, and naming is important (even for a an internet example).
It's all OK to explain what currying is, but without an explanation as to why one would want to go to all this trouble and extra complexity is necessary to support the article.
Thanks, it would be really cool to know if there's some real world case where this is a solution.
I often use it when I want to use a function in array.map but it takes more than one argument. Supply the initial argument(s) then use the returned function in map.
That makes a bunch of sense, thanks!
It's just pure vanilla JavaScript. Fundamental principle of functional programming so any language that treats functions as arguments can do this - not a JavaScript mess 😉
Interesting article. Never heard of that. But what about the browser support? And from which EcmaScript is it being supported?
hi, nice article.
can you please also provide some use case for more clarification.
This thing is a pure brain fck for Java devs, like me. Rest, please keep unveiling secrets of this js.
AHH ! The JavaScript mess.....