Driver code
const join = (a, b, c) => {
return `${a}_${b}_${c}`
}
const curriedJoin = curry(join)
curriedJoin(1)(2)(3)
Implementation
function curry(fn) {
return function curried(...args) {
if (args.length >= fn.length) return fn(...args)
return function(...nextArgs) {
return curried(...args, ...nextArgs)
}
}
}
I'm having trouble understanding this implementation of a curried function in JavaScript, specifically the recursive case.
I think that curried is returning an anonymous function that expects some arguments, and wraps another call of curried passing the last call and current call's combined arguments.
I don't understand how on the next call of curry, as in curriedJoin(2), the execution jumps to the wrapped function's return curried(...args, ...nextArgs) line. Why does the execution not run through the function line by line rather than "remembering" to call this anonymous function?
I have a vague understanding of closures and I understand that the execution context is alive after the function returns - I think this is where my confusion lies.