1.0.3 • Published 4 years ago

curry-function v1.0.3

Weekly downloads
3
License
ISC
Repository
github
Last release
4 years ago

🍛 Curry-function

Curry-function. Passed a function f, returns a curried variant of f. Lightweight and minimal. Handles arbitrary number of arguments and supports overloading. Similar to curry, but does not use dangerous eval.

Usage

Install locally with npm:

npm i curry-function

Import:

const curry = require('curry-function');

or

import curry from 'curry-function';

API

Syntax

curry(func);

Parameters

func

Required. The function to be curried.

Return

A new function. The curried variant of the argument function.

Example:

const curry = require('curry-function');

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

const curriedSum = curry(sum);

const applyFirst = curriedSum(1); // returns function
const applySecond = applyFirst(2); // returns function
const applyAll = applySecond(3); // returns 6

Overloading:

curriedSum(1, 2);

// returns function, is equivalent to:

curriedSum(1)(2);

// argument function can be called as usual:

curriedSum(1, 2, 3);

// returns 6, is equivalent to:

curriedSum(1)(2)(3);

// is equivalent to:

sum(1, 2, 3)

Test

Tested with AVA.

npm test

or

npx ava

Currying

Given a function f with n parameters, curry returns n functions, each taking exactly one of f's parameters, one parameter by one. The final function executes f's code. For an overview on the uses of currying see this article.

To illustrate:

// not curried expression

const notCurried = function(x, y, z) {
  return z + y + z;
}

// curried expression

const curried = function(x) {
  return function(y) {
    return function(z) {
      return x + y + z;
    }
  }
};

// not curried arrow

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

// curried arrow

const curried = (x) => (y) => (z) => x + y + z;