functions

Methods

(static) curry(fn, nopt) → {function}

Source:

Curries a function with an arbitrary number of declared arguments.

The result is a single-argument function. If the original function had more than one argument, then that single-argument function will return another single-argument function. If the original function had more than two arguments, then that single-argument function will return yet another single-argument function. This continues until there are as many nested single-argument functions as there were arguments in the original function. The most deeply nested function will then gather all of the arguments from the other functions and execute the original function.

To illustrate with an example, currying this function

const sum3 = (a, b, c) => a + b + c

will produce a function that behaves like this function

const curriedSum = a => b => c => a + b + c

Here are some examples of usage.

const sum = curry((a, b, c) => a + b + c)
const add3 = sum (3)
const add34 = sum (3) (4)

const result1 = sum (1) (2) (3)
const result2 = add3 (4) (5)
const result3 = add34 (2)

console.log(result1)  // 6
console.log(result2)  // 12
console.log(result3)  // 9

To know how many nested functions to create, curry by default reads the number of arguments in the original function and creates that many. This means that some argument types don't work well with curry. Optional and rest arguments are not included in the argument total, so currying this function

const sum2plus = (a, b, ...ns) => a + b + ns.reduce((x, y) => x + y, 0)

will produce a function that acts like this one

const bad = a => b => a + b + ns.reduce((x, y) => x + y, 0)

This, of course, will throw an error when called because ns is set to undefined and you can't call reduce on that.

If you need to curry a function that uses rest or optional parameters, a second argument is available that explicitly dictates how many parameters to curry. This will essentially change optional and rest parameters into regular parameters, as there is no such thing as an optional or rest parameter in a curried function, but it does allow working with such functions without throwing errors.

// Function to calculate sum of an arbitrary number of values
const sum = (...ns) => ns.reduce((a, b) => a + b, 0)
// Curried function to calculate sum of exactly 2 and 4 values, respectively
const sum2 = curry(sum, 2) // same as sum2 = x => y => sum(x, y)
const sum4 = curry(sum, 4) // same as sum4 = x => y => z => w => sum(x, y, z, w)

So a curried rest parameter function still calls the original function internally, but it does not allow a varying number of arguments.

This optional second argument can be used on regular functions as well, currying fewer or more arguments than the function actually has. There is likely little reason to do this, however.

There are other currying functions in this library, namely curry2, curry3, curry4, and curry5. Each of these functions curry a specific number of parameters and are equivalent to, respectively, curry(fn, 2), curry(fn, 3), curry(fn, 4), and curry(fn, 5). They're a bit simpler and so a little faster and a little easier to debug (because of more straightforward stack traces). They can be considered if you know exactly how many parameters need to be curried in a given function.

Most of the functions in this library call curry on any multi-argument functions that they accept, and most of the functions in this library that are multi-argument are curried themselves. The library expects everything to be a single-argument function.

Parameters:
Name Type Attributes Default Description
fn function

A function of any number of parameters.

n number <optional>
fn.length

The number of parameters to curry. This defaults to the number of parameters in function fn, not including any optional or rest parameters.

Returns:

A curried function of n of parameters.

Type
function

(static) curry2(fn) → {function}

Source:

Curries a two-argument function.

The result will always be a two-argument curried function. Whether or not the actual function takes two arguments, less than two, or more than two doesn't matter. If it takes less, the extra arguments will be ignored; if it takes more, the arguments past the second will all be set to undefined. This is normal JavaScript behavior.

Unlike curry, the curried function will not fail if one of the argument is optional or a rest argument. In fact, currying a function takes away any optional or rest nature of any arguments. The curried function will take exactly two arguments, whatever kind they are.

const sum = curry2((a, b) => a + b)

// This is the only possible calling format.
const result = sum (1) (2)

// This will not work. It is the same as sum (1) as the 2nd argument is ignored.
const partial = sum (1, 2)

console.log(result)  // 3
console.log(partial) // function curry2(b)
Parameters:
Name Type Description
fn function

A function of two arguments.

Returns:

A curried function of two arguments.

Type
function

(static) curry3(fn) → {function}

Source:

Curries a three-argument function.

The result will always be a three-argument curried function. Whether or not the actual function takes three arguments, less than three, or more than three doesn't matter. If it takes less, the extra arguments will be ignored; if it takes more, the arguments past the second will all be set to undefined. This is normal JavaScript behavior.

Unlike curry, the curried function will not fail if one of the argument is optional or a rest argument. In fact, currying a function takes away any optional or rest nature of any arguments. The curried function will take exactly three arguments, whatever kind they are.

const sum = curry3((a, b, c) => a + b + c)

// This is the only possible calling format.
const result = sum (1) (2) (3)

// This will not work. It is the same as sum (1) (3) as the 2nd argument is ignored.
const part1 = sum (1, 2) (3)
// This will also not work. It's the same as sum (1) (2) as the 3rd argument is ignored.
const part2 = sum (1) (2, 3)
// This is right out.
const part3 = sum (1, 2, 3)

console.log(result) // 6
console.log(part1)  // function curry3(c)
console.log(part2)  // function curry3(c)
console.log(part3)  // function curry3(b)
Parameters:
Name Type Description
fn function

A function of three arguments.

Returns:

A curried function of three arguments.

Type
function

(static) curry4(fn) → {function}

Source:

Curries a four-argument function.

The result will always be a four-argument curried function. Whether or not the actual function takes four arguments, less than four, or more than four doesn't matter. If it takes less, the extra arguments will be ignored; if it takes more, the arguments past the second will all be set to undefined. This is normal JavaScript behavior.

Unlike curry, the curried function will not fail if one of the argument is optional or a rest argument. In fact, currying a function takes away any optional or rest nature of any arguments. The curried function will take exactly four arguments, whatever kind they are.

const sum = curry4((a, b, c, d) => a + b + c + d)

// This is the only possible calling format.
const result = sum (1) (2) (3) (4)

// This will not work. It is the same as sum (1) (3) (4) as the 2nd argument is ignored.
const part1 = sum (1, 2) (3) (4)
// This will not work. It's the same as sum (1) (2) (4) as the 3rd argument is ignored.
const part2 = sum(1) (2, 3) (4)
// This is right out.
const part3 = sum (1, 2, 3, 4)

console.log(result) // 10
console.log(part1)  // function curry3(d)
console.log(part2)  // function curry3(d)
console.log(part3)  // function curry3(b)
Parameters:
Name Type Description
fn function

A function of four arguments.

Returns:

A curried function of four arguments.

Type
function

(static) curry5(fn) → {function}

Source:

Curries a five-argument function.

The result will always be a five-argument curried function. Whether or not the actual function takes five arguments, less than five, or more than five doesn't matter. If it takes less, the extra arguments will be ignored; if it takes more, the arguments past the second will all be set to undefined. This is normal JavaScript behavior.

Unlike curry, the curried function will not fail if one of the argument is optional or a rest argument. In fact, currying a function takes away any optional or rest nature of any arguments. The curried function will take exactly five arguments, whatever kind they are.

const sum = curry5((a, b, c, d, e) => a + b + c + d + e)

// This is the only possible calling format.
const result = sum (1) (2) (3) (4) (5)

// This is the same as sum(1)(3)(4)(5) as the 2nd argument is ignored.
const part1 = sum (1, 2) (3) (4) (5)
// This is the same as sum (1) (2) (4) (5) as the 3rd argument is ignored.
const part2 = sum (1) (2, 3) (4) (5)
// This is right out.
const part3 = sum (1, 2, 3, 4, 5)

console.log(result) // 15
console.log(part1)  // function curry3(e)
console.log(part2)  // function curry3(e)
console.log(part3)  // function curry3(b)
Parameters:
Name Type Description
fn function

A function of five arguments.

Returns:

A curried function of five arguments.

Type
function

(static) curryv(fn) → {function}

Source:

Produces a variadiccurried function out of a regular function.

Variadic currying means that the curried function is a function of any number of arguments, rather than the normal single-argument function. This variadic function can accept any or all remaining arguments. If it is given fewer than the total number of arguments, it will return another variadic curried function. This continues until the total arguments provided to all of the variadic curried funcitons matches the total arguments in the original function; at this point the return value is actually calculated and returned.

To make this happen, this currying function reads the number of arguments from the original function. However, this makes it incapable of reacting to some ways of providing those arguments. It will not work on functions that declare no arguments and then use arguments to determine what was actually passed. Rest parameters and default parameters are not counted, so using this function with them will produce curried functions that expect a number of arguments in total that do not include those parameters (or anything after them).

If a function has already been curried by this function, or if it has less than two arguments, passing that function to curry will be a no-op (the same function will be returned without modification). A corollary is that curry will not curry a regularly curried function (one in which no parameter list has more than one parameter).

This is largely a convenience function. It doesn't have a lot of compatibility with the rest of this library. Most other functions either expect single-argument (or regular curried) functions, and if they accept multi-argument functions, they call curry on them—and curry doesn't work on the variadic functions that curryv creates.

const sum = curry((a, b, c) => a + b + c)

// Each of the following are equivalent
const res1 = sum (1, 2, 3)
const res2 = sum (1, 2) (3)
const res3 = sum (1) (2, 3)
const res4 = sum () (1, 2, 3)
const res5 = sum (1) (2)(3)

console.log(res1)  // 6
console.log(res2)  // 6
console.log(res3)  // 6
console.log(res4)  // 6
console.log(res5)  // 6
Parameters:
Name Type Description
fn function

A function of any number of arguments.

Returns:

A new function that behaves as fn, unless it's called with fewer arguments than the number given in fn's definition. In this case, the new function will return another new function that accepts more of the arguments.

Type
function

(static) partial(fn, …args) → {function}

Source:

Partially applies arguments to a function, resulting in another function that takes the rest of the arguments.

Unlike curryv, this function also takes some arguments (hence it being partial application and not currying). However, it is not the same as currying a function and then passing it an initial set of argumetns. It returns a function that takes more arguments, and calling that function will result in an answer whether the expected number of arguments have been provided or not.

Note that this function always returns a function, even when provided enough arguments to satisfy the function it partially completes. Another call (presumably with empty parentheses) will have to be made to get the return value.

partial breaks a function application up into exactly two calls, no more, no less. It's a convenience function. For more complex partial application, choose any curry function and pass it a few arguments.

const sum = partial((a, b, c) => a + b + c, 1, 2)

const part3 = sum(3)
const part10 = sum(10)
const part1001 = sum(1001)

console.log(part3)    // 6
console.log(part10)   // 13
console.log(part1001) // 1004

// This still returns a function even though three arguments have been supplied.
const prod = partial((a, b, c) => a * b * c, 2, 3, 4)

// The function still needs to be invoked to get the answer.
console.log(prod())    // 24
Parameters:
Name Type Attributes Description
fn function

The function to be partially applied.

args * <repeatable>

The arguments to partially apply to fn.

Returns:

A new function that takes a variable number of arguments and applies them after args are applied.

Type
function

(static) uncurry(fn, n) → {function}

Source:

Reverses the currying on a function, returning a function of multiple parameters.

Since curried functions are all single-argument, there's no way to automatically read the number of arguments in the entire function. For this reason, uncurry has a second argument like curry, but in this case, the argument is not optional.

The returned function takes n arguments. Like any other JavaScript function, if you pass more arguments than that, the extra will be ignored, and if you pass fewer, the unspecified arguments will be set to undefined.

It is the responsibility of the caller of uncurry to get n right, as uncurry will faithfully produce an n-parameter function even if that's not actually the right number of parameters. Getting n wrong can lead to functions that return functions rather than expected values (if n is too low) or functions that throw errors when they're called (if n is to high).

const curriedSum = a => b => c => a + b + c
const sum = uncurry(sum, 3)
const result = sum(1, 2, 3)
console.log(sum) // 6
Parameters:
Name Type Description
fn function

A curried function of n parameters.

n number

The number of parameters in the curried function fn.

Returns:

An uncurried function of n parameters that does the same thing as the original function fn.

Type
function