📜  es6 currying - Javascript (1)

📅  最后修改于: 2023-12-03 15:14:53.702000             🧑  作者: Mango

ES6 Currying - Javascript

Currying is a technique in functional programming where a function that takes multiple arguments is transformed into a series of functions that take a single argument. This allows for cleaner and more modular code, as well as more flexible and reusable functions.

In ES6, currying can be achieved using arrow functions and the spread operator. Consider the following example:

const add = x => y => x + y;

const addFive = add(5);
console.log(addFive(3)); // outputs 8

In this example, the add function is defined using an arrow function. It takes in a single argument x, and returns a new function that takes in another single argument y, and returns the sum of x and y.

To create a new curried function that adds 5 to its input, we simply pass 5 as the argument to add, which returns a new function that adds 5 to its input.

The result is a new function addFive that takes in a single argument and returns the sum of that argument and 5. This allows us to easily reuse and compose functions in our code.

More Complex Currying

Currying can also be applied to more complex functions that take in multiple arguments. Consider the following example:

const curry = func => (...args) =>
  args.length >= func.length
    ? func(...args)
    : curry(func.bind(null, ...args));

const add = (x, y, z) => x + y + z;

const curriedAdd = curry(add);
console.log(curriedAdd(1)(2)(3)); // outputs 6
console.log(curriedAdd(1, 2)(3)); // outputs 6
console.log(curriedAdd(1)(2, 3)); // outputs 6

In this example, we define a generic curry function that takes in another function as an argument. It initially creates and returns a new function that takes in a variable number of arguments using the spread operator.

This new function checks whether the number of arguments passed in is greater than or equal to the number of arguments expected by the original function (func). If so, the original function is called with the provided arguments and the result is returned.

Otherwise, the function returns a new function that is bound to the original function with the provided arguments using the bind method. This allows us to build up the call to the original function over multiple invocations, until all of the required arguments have been provided.

With this curry function defined, we can create a curried version of add that takes in three arguments x, y, and z.

We can then call curriedAdd with various combinations of arguments, and it will return a new curried function that takes in the remaining arguments. This allows us to easily reuse and compose functions in more complex scenarios.

Conclusion

ES6 currying provides a powerful tool for creating more modular, flexible, and reusable functions in Javascript. By transforming functions that take multiple arguments into a series of functions that take a single argument, we can simplify our code and make it more expressive.

With arrow functions and the spread operator, we can easily define and apply curried functions in our code, allowing us to take full advantage of functional programming principles in Javascript.