[object Object]

FE Interview Question - Implement Currying

Instructions

Create a function sum that implements currying. Currying is a transformation of functions that translates a function from being callable as f(x, y, z) into callable as f(x)(y)(z). Currying doesn’t call a function. It just transforms it.

In this question, Implement a sum function that can take n number of parameters and calls and outputs the resulting sum of all the numbers that are passed into it.

Example

// sum implementation hereconst sum = () => {
    ...
    ...
}
console.log(sum(1)(2)(5)());

// Output: 8

Here, the output will be 8 because the sum function takes in 12, and 5 outputting the sum of all the three numbers as 8.

What is currying?

Currying is a technique in JavaScript where a function with multiple arguments is transformed into a sequence of functions with a single argument. The result of each function is a new function that takes the next argument in the sequence until all arguments have been collected and the original function can be applied. This allows for more flexible function composition and is often used in functional programming.

In this Example:

const sum = (a) => {
  const add = (b) => {
    if (b) {
      return sum(a + b);
    } else {
      return a;
    }
  };
  return add;
}

This is a programming task that requires creating a function called sum that implements currying. Currying is a technique where a function with multiple arguments is transformed into a sequence of functions with a single argument. The sum function should take in n number of parameters and calls and output the resulting sum of all the numbers that are passed into it.

In the given example, the sum function is first called with 1, which returns a new function that expects 2 as its argument. This new function returns another function that expects 5 as its argument. Finally, this last function is called with an empty argument list, which causes the sum of 1, 2, and 5 to be returned and printed to the console using console.log().

A sample implementation of the sum function is provided in the example as well, which uses recursion to add up the numbers.

Sample Answer 2

function sum() {
  let args = Array.prototype.slice.call(arguments);
  let adder = function() {
    args.push(...arguments);
    return adder;
  };
  adder.valueOf = function() {
    return args.reduce((a, b) => a + b);
  };
  return adder;
}

This implementation of the sum function also uses currying to add up the numbers passed to it. It takes in an arbitrary number of arguments and returns a new function that can be called with additional arguments. When the returned function is called with no arguments, it returns the sum of all the arguments that have been passed in so far.

The implementation uses a closure to keep track of the arguments that have been passed in. It also overrides the valueOf function of the returned function to return the sum of the arguments.

This implementation allows for more flexible usage of the sum function, as arguments can be passed in one at a time or all at once. It also allows for chaining of function calls, as the returned function can be called multiple times with different arguments.

← Go home