Skip to main content

curry()

curry<Args, Return>(func, arity): CurriedFunction<Args, Return>

Creates a curried function that accepts arguments one at a time.

note

Supports partial application with multiple arguments at once.


Type Parametersโ€‹

Args: Args extends unknown[]โ€‹

The argument types of the function.

Return: Returnโ€‹

The return type of the function.


Parametersโ€‹

func: (...args) => Returnโ€‹

The function to curry.

arity: number = func.lengthโ€‹

The arity of func (defaults to func.length).


Returns: CurriedFunction<Args, Return>โ€‹

The curried function.


Throwsโ€‹

RangeError If arity is negative or not an integer.


Sinceโ€‹

2.0.0


Noteโ€‹

When arity is 0, the function is invoked immediately upon first call.


Also known asโ€‹

curry (Lodash, es-toolkit, Ramda) ยท โŒ (Remeda, Radashi, Effect, Modern Dash, Antfu)


Exampleโ€‹

const add = (a: number, b: number, c: number) => a + b + c;
const curriedAdd = curry(add);

curriedAdd(1)(2)(3); // => 6
curriedAdd(1, 2)(3); // => 6
curriedAdd(1)(2, 3); // => 6
curriedAdd(1, 2, 3); // => 6

// Partial application
const add10 = curriedAdd(10);
add10(5)(3); // => 18

// Custom arity
const fn = (...args: number[]) => args.reduce((a, b) => a + b, 0);
const curried = curry(fn, 3);
curried(1)(2)(3); // => 6

How it works?โ€‹

Transforms a function to accept arguments one at a time (or in groups). Returns a new function for each argument until all are provided, then executes.

Multiple Call Stylesโ€‹

Partial Applicationโ€‹


Use Casesโ€‹

Partial Application for reusable functions ๐Ÿ“Œโ€‹

Create reusable partially applied functions. Essential for functional programming patterns.

import { curry } from "pithos/arkhe/function/curry";

const add = (a: number, b: number, c: number) => a + b + c;
const curriedAdd = curry(add);

// Apply arguments one at a time
const add5 = curriedAdd(5);
const add5and10 = add5(10);
console.log(add5and10(3)); // 18

// Or apply multiple arguments at once
console.log(curriedAdd(1, 2, 3)); // 6
console.log(curriedAdd(1)(2, 3)); // 6

Event Handlers with pre-configured context ๐Ÿ“Œโ€‹

Create event handlers with pre-configured parameters. Critical for UI component callbacks.

import { curry } from "pithos/arkhe/function/curry";

const handleClick = (action: string, category: string, event: MouseEvent) => {
analytics.track({ action, category, x: event.clientX, y: event.clientY });
};

const curriedHandler = curry(handleClick);

// Pre-configure handlers for different buttons
const handleNavClick = curriedHandler("click", "navigation");
const handleFormClick = curriedHandler("click", "form");

// Use in components
navButton.addEventListener("click", handleNavClick);
submitButton.addEventListener("click", handleFormClick);

API Request buildersโ€‹

Build configurable API request functions. Important for consistent API interactions.

import { curry } from "pithos/arkhe/function/curry";

const fetchResource = async (
baseUrl: string,
headers: Record<string, string>,
endpoint: string
) => {
const response = await fetch(`${baseUrl}${endpoint}`, { headers });
return response.json();
};

const curriedFetch = curry(fetchResource);

// Create pre-configured fetchers
const apiClient = curriedFetch("https://api.example.com", {
Authorization: "Bearer token",
"Content-Type": "application/json",
});

// Use the configured client
const users = await apiClient("/users");
const posts = await apiClient("/posts");