Facebook Pixel
Solving sum with a Generic Curry Function

What is the difference between generic curry and the Amazon sum question?

The answer

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.

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)); }; }

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?

Your learning library

Small steps. Big possibilities.

Find your next course. Build something great.

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

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.