Skip to content

curry

Transforms a multi-argument function into a chain of single-argument functions (Haskell-style currying). Supports up to 5 arguments.

The inverse operation of applying all arguments at once: curry(fn)(a)(b) is equivalent to fn(a, b).

Available since v2.0.0

import { curry } from '@helpers4/function';
// or, from the all-in-one package (same code, one install):
import { curry } from 'helpers4/function';
curry<A, R>(fn: function): function
ParameterTypeDescription
fnfunctionThe function to curry

function — A curried version of fn

Curry a 2-argument function to build specialised versions.

const add = curry((a: number, b: number) => a + b);
const add5 = add(5);

add5(3);  // => 8
add5(10); // => 15

Curry enables point-free style when composing pipelines.

const clamp = curry((min: number, max: number, v: number) =>
  Math.min(Math.max(v, min), max)
);
const clamp0to100 = clamp(0)(100);

clamp0to100(42);  // => 42
clamp0to100(-5);  // => 0
clamp0to100(150); // => 100

View source on GitHub