Nuclear Powered Marshmallows

    curry-function

    1.0.3 • Public • Published

    🍛 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;

    Install

    npm i curry-function

    DownloadsWeekly Downloads

    8

    Version

    1.0.3

    License

    ISC

    Unpacked Size

    4.27 kB

    Total Files

    4

    Last publish

    Collaborators

    • avatar