Facebook Pixel

Can you solve sum(1)(2)(3) 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)); }; }

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in Solving sum with a Generic Curry Function

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.

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.

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

Want to upskill yourself?

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0