Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

curry(fn, [arity]) ⇒ function

Transforms a function of N arguments in such a way that it can be called as a chain of N functions each with a single argument (arity: 1).

Returns: function - A curried equivalent of the provided function.
Throws:

  • TypeError Throws if fn is not a function.
  • TypeError Throws if arity is not a number but not undefined.
Param Type Default Description
fn function The initial function to be curried.
[arity] Number fn.length The arity of the provided function. Useful in cases that arity cannot be determined by fn.length. As of ES2015 when a function has a rest parameter or at least one parameter with default value, the fn.length is not properly calculated.

Example

const add = curry((a, b) => a + b);
const addOne = add(1);
addOne(2); // => 3

// Provide arity as second argument in cases that it cannot be determined.
const add = curry((a = 0, ...args) => a + args[0] + args[1], 3);
const addOne = add(1);
const addTwo = addOne(2);
addTwo(3); // => 6