Skip to content

Function: curry()

ts
function curry(fn): (...args) => (...args) => any;

Creates a curried version of a function that can be called with partial arguments. 创建函数的柯里化版本,可以使用部分参数调用。

Currying transforms a function that takes multiple arguments into a sequence of functions that each take a single argument. This enables partial application and function composition patterns. 柯里化将接受多个参数的函数转换为一系列每个都接受单个参数的函数。 这使得部分应用和函数组合模式成为可能。

Parameters

ParameterTypeDescription
fn(...args) => anyThe function to be curried / 要柯里化的函数

Returns

A curried function / 柯里化的函数

(...args) => (...args) => any

Example

typescript
const add = (a, b, c) => a + b + c
const curriedAdd = curry(add)

curriedAdd(5)(3)(2) // => 10
curriedAdd(1)(2)(3) // => 6
curriedAdd(1, 2, 3) // => 6

const multiply = (a, b, c) => a * b * c
const double = curry(multiply)(2)
double(3)(4) // => 24

const formatMessage = curry((prefix, type, message) => `[${prefix}] ${type}: ${message}`)
const logError = formatMessage('APP')('ERROR')
logError('Something went wrong') // => "[APP] ERROR: Something went wrong"

Since

1.0.0

Released under the MIT License.