Aller au contenu principal

curry()

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

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

remarque

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");