How do you adapt curry for infinite chaining?
Instead of checking args.length >= fn.length, check if no arguments were passed (args.length === 0). If so, return the accumulated value. Otherwise, return a new curried function with the updated accumulator.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Solving sum with a Generic Curry Function
The standard generic curry does not directly apply (it expects a fixed number of arguments). But you can adapt it for infinite chaining: function infiniteCurry(fn, initial) { return function(...args) { if (args.length === 0) return initial; return infiniteCurry(fn, fn(initial, ...args)); }; }
Generic curry expects a fixed number of arguments (fn.length) and calls the original when enough are collected. The Amazon sum allows infinite chaining and terminates on an empty call. The concept (closures, accumulation) is the same but the termination mechanism differs.
fn.length is the number of parameters declared in the original function. The curry function uses it to know when enough arguments have been collected. For (a, b) => a + b, fn.length is 2.
Still have questions?
Browse all our FAQs or reach out to our support team
