Search⌘ K
AI Features

Solution Review: Let's Curry!

Learn how to convert any JavaScript function into a curried version by understanding recursive argument handling. This lesson walks you through implementing currying that collects arguments until complete, then executes the original function, enhancing your functional programming skills.

We'll cover the following...

Solution

Javascript (babel-node)
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 ...

Javascript (babel-node)
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, ...