The task is to implement a curry function that takes a function and returns a curried one.
Currying is a transformation of functions that take multiple arguments into a sequence of functions that can take those arguments, one at a time. The final result of the curried version is the same as the original version.
When the curried function is first called, it collects the arguments given. If the arguments are as many as the original function, the original function is executed right away
funtion curry(fn) {
return function curried(...args){
if(args.length >= fn.length) {
return fn(...args)
}
}
}
If the arguments are less than the original function, it returns a new function that waits for the remaining arguments.
else {
return (...nextArgs) => curried(...args, ...nextArgs)
}
As long as the arguments are not enough, the curried function doesn't run the original function yet.
The final code:
function curry(fn) {
// your code here
return function curried(...args) {
if(args.length >= fn.length) {
return fn(...args)
} else {
return (...nextArgs) => curried(...args, ...nextArgs)
}
}
}
That's all folks!
Top comments (0)