Solution Review: Let's Curry!
This lesson will explain the solution to the problem in the previous lesson.
We'll cover the following...
Solution #
Press + to interact
function currying(func) {function curriedfunc(...args) {if(args.length >= func.length) {return func(...args);} else {return function(...next) {return curriedfunc(...args,...next);}}}return curriedfunc;}function multiply(a, b, c) {return a*b*c;}let curried = currying(multiply);console.log(curried(2)(3)(4))console.log(curried(2,3)(4))console.log(curried(2,3,4))console.log(curried(5)(6,7))
Explanation #
A simple approach for converting the multiply
function to a curried function is:
Press + to interact
function multiply(a){return function(b){return function(c){return a*b*c}}}console.log(multiply(2)(3)(4))
However, the question requires you to write a function, currying
, that takes any function and converts it into a currying function. So how do we do that?
-
First, we are passing the function,
func
, to our function,currying
(line 1).function currying(func)
-
The next step is to think about the output. Re-stating the problem, we want to return a currying function. Hence, we define the function ...