JavaScript is filled with cool features, and one of the most mind-blowing concepts is currying. Donβt worry if it sounds fancyβby the end of this b...
For further actions, you may consider blocking this person and/or reporting abuse
Your trick question goes beyond simple currying to add recursion.
add(1)
returns a function that takes a single argument, call thisaddOne
. LIke this:This returns a closure where
a
is assigned1
.add(1)(2)
is equivalent toaddOne(2)
where2
is theb
argument. Asb
exists and is not0
, this returns the output ofadd(1 + 2)
readadd(3)
. This makes3
thea
in a new closure, so the function it returns could be calledaddThree
.add(1)(2)(3)
then is equivalent toaddThree(3)
which hasa === 3
from the closure returned earlier andb == 3
from the argument. So again we recurse, returningadd(3 + 3)
oraddSix
.So
add(1)(2)(3)(4)
isaddSix(4)
whereb === 4
hence it returnsadd(6 + 4)
which we can calladdTen
.Finally
add(1)(2)(3)(4)()
callsaddTen()
with no arguments, which halts the recursion and just returns thea
value (the sum) from the closure, which is10
.We can see this more clearly like this:
Easy peasy β if you understand currying, closures, and recursion.
P.S. Your article doesn't really explore why currying is so useful (which is why ALL functions in Haskell are curried). Why would I need an
addThree
function?The most obvious answer is the use of
pipe
orcompose
. Pipe is easier, so here's an example assuming curried arithmetic functions:Note, all functions passed to pipe (except the first one) must be unary, i.e., take only one parameter.
Thanks for sharing your views.
Wow, Nice explanation @chasm
This is well writen and the examples are very easy to follow and illustrate your points well. I have read a few articles about currying from time to time, but it always feels like a bit of mental gymnastics is required to use it. I can follow the examples, but its not something I can look at and instantly say "thats whats going on". Maybe it would become more intuitive if I used it more. Anyway, good write up.
Thanks @davepile
`
`
const add = (a) => (b) => b ? add(a + b) : a;
console.log(add(1)(2)(3)(4)()); // β
`
`
It's answer would be : 10 ( if I uderstand the concept clearly)
Yes it's correct !!
What's your approach to solve this.
Absolutely incredible! π».
Thanks @aniruddhadak
It's fantastic.
How does utility function works?? It's confusing
What does this code do
fn(...args)
: curry(fn.bind(null, ...args));
This is actually a pretty smart bit of code. It allows you to curry any JavaScript function (remember that all functions must take at least one parameter, else they are effectively constants).
The function itself is curried, so you can pass in your function (
sum
) and get back a function that takes one or more arguments, which we collect into an array calledargs
:(...args)
.We can check how many arguments
sum
expects withlength
, hencefn.length
here returns3
becausesum(a, b, c)
.So when the curried
sum
is called, we check if the number of arguments passed in (args
) is equal to the number of parameters expected (fn.length
). If all the arguments are there, then we just call thesum
function with them, spreading theargs
back out:fn(...args)
.But if too few arguments have been passed, e.g.,
sum(1, 2)
, then we bind those arguments to their parameters (here,a = 1
andb = 2
) and return a closure that expects the third argument (c
). When it gets that argument, it will meet the conditionfn.length = args.length
, so it will simply call the function with the remaining argument(s).Hope that helps.
I call that a fonctional paradigme.