Design & Download
Your JS Library
in Seconds!!!

Snippet: curry() & rcurry() - Partial Application

Written by Christopher West (cwest) on May 03, 2016.
Creates a wrapper function that will successively store the arguments passed. If the number of arguments is at least the specified arity, the wrapped function will be called with all stored arguments, otherwise the wrapper function will be returned.
function curry(fn, opt_arity) {
  opt_arity = opt_arity || fn.length;
  return function wrap() {
    var args = [];
    return (function inner() {
      return (args = args.concat(slice(arguments))).length < opt_arity
        ? inner
        : fn.apply(this, args);
    }).apply(this, arguments);
  };
}

function rcurry(fn, opt_arity) {
  opt_arity = opt_arity || fn.length;
  return function wrap() {
    var args = [];
    return (function inner() {
      return (args = slice(arguments).concat(args)).length < opt_arity
        ? inner
        : fn.apply(this, args);
    }).apply(this, arguments);
  };
}

curry() API Documentation

Description

Creates a wrapper function that will successively store the arguments passed. If the number of arguments is at least the specified arity, the wrapped function will be called with all stored arguments, otherwise the wrapper function will be returned.

Parameters

  1. fn {Function}
    The function that will be called once the wrapper function receives enough arguments.
  2. opt_arity {number}
    Optional. Defaults to fn.length. If specified this will be the minimum amount of arguments which must be received by the wrapper function before calling fn().

Returns

The wrapper function which when called will store the arguments passed. When calling this function...
If the amount of arguments passed is at least the arity fn() will be called using all of the passed arguments and the return value will be the result of that function. If the amount of arguments is still less than the arity the wrapper function will be returned.

Example

A nice utility function to have because it gives us the ability to preset the left-most arguments in a function:

  function sum(a) {
    for (var i = 1; i < arguments.length; i++) {
      a += arguments[i];
    }
    return a;
  }

  var sum4 = YourJS.curry(sum, 4);
  console.log(sum4(1)(2)(3)(4));  // Results in 10
  console.log(sum4(1, 2)(3)(4));  // Results in 10
  console.log(sum4(1, 2, 3)(4));  // Results in 10
  console.log(sum4(1, 2, 3, 4));  // Results in 10
  

rcurry() API Documentation

Description

Creates a wrapper function that will prepend and store the arguments passed. If the number of arguments is at least the specified arity, the wrapped function will be called with all stored arguments, otherwise the wrapper function will be returned.

Parameters

  1. fn {Function}
    The function that will be called once the wrapper function receives enough arguments.
  2. opt_arity {number}
    Optional. Defaults to fn.length. If specified this will be the minimum amount of arguments which must be received by the wrapper function before calling fn().

Returns

The wrapper function which when called will store the arguments passed. When calling this function...
If the amount of arguments passed is at least the arity fn() will be called using all of the passed arguments and the return value will be the result of that function. If the amount of arguments is still less than the arity the wrapper function will be returned.