preludejs/Func API
Complete API reference for preludejs/Func
preludejs/Func API Reference
Core
apply ƒ
Returns the application of the supplied list as arguments to the supplied function.
function apply<A extends readonly unknown[], R>(fn: (...args: A) => R, args: A): RParameters
| Parameter | Type | Description |
|---|---|---|
fn | (...args: A) => R | |
args | A |
Example
apply((a: number, b: number) => a + b, [2, 3]) //=> 5compose ƒ
Performs right-to-left function composition. The rightmost function may have any arity; the remaining functions must be unary.
function compose<Fns extends readonly AnyFn[]>(...fns: Fns): Compose<Fns>Parameters
| Parameter | Type | Description |
|---|---|---|
fns | Fns |
Example
compose((x: number) => x + 1, (x: number) => -x)(3) //=> -2const ƒ
Returns the first supplied value and ignores the second.
function const<A>(value: A): <B>(_ignored: B) => A
function const<A, B>(value: A, _ignored: B): AParameters
| Parameter | Type | Description |
|---|---|---|
value | A |
Example
constant('hello')('world') //=> 'hello'
constant('hello', 'world') //=> 'hello'curry ƒ
Returns a curried version of the supplied function.
function curry<A extends readonly unknown[], R>(fn: (...args: A) => R): Curried<A, R>Parameters
| Parameter | Type | Description |
|---|---|---|
fn | (...args: A) => R |
Example
curry((a: number, b: number) => a + b)(1)(2) //=> 3deny ƒ
Denies the result of a predicate function.
function deny<A extends readonly unknown[], R>(fn: (...args: A) => R): (...args: A) => booleanParameters
| Parameter | Type | Description |
|---|---|---|
fn | (...args: A) => R |
Example
deny((x: number) => x > 2)(2) //=> truefix ƒ
Fix-point function for anonymous recursion, implemented with the https://en.wikipedia.org/wiki/Fixed-point_combinator#Y_combinator Y combinator.
function fix<A extends readonly unknown[], R>(fn: (recur: (...args: A) => R) => (...args: A) => R): (...args: A) => RParameters
| Parameter | Type | Description |
|---|---|---|
fn | (recur: (...args: A) => R) => (...args: A) => R |
Example
fix<[number], number>((recur) => (n) => (n <= 1 ? 1 : recur(n - 1) + recur(n - 2)))(9) //=> 55flip ƒ
Returns a function with the first two arguments flipped.
function flip<A extends readonly unknown[], R>(fn: (...args: A) => R): (...args: Reverse<A>) => RParameters
| Parameter | Type | Description |
|---|---|---|
fn | (...args: A) => R |
Example
flip(Math.pow)(2, 3) //=> 9memoize ƒ
Caches computed results, speeding up later calls with the same arguments.
function memoize<A extends readonly unknown[], R>(fn: (...args: A) => R): (...args: A) => RParameters
| Parameter | Type | Description |
|---|---|---|
fn | (...args: A) => R |
Example
memoize((n: number) => n * 2)(2) //=> 4uncurry ƒ
Applies an array of arguments to a function, which is especially handy for curried functions.
function uncurry<A extends readonly unknown[], R>(fn: (...args: A) => R, args: A): R
function uncurry<A extends readonly unknown[], R>(fn: (...args: A) => R): (args: A) => RParameters
| Parameter | Type | Description |
|---|---|---|
fn | (...args: A) => R | |
args | A |
Example
uncurry(curry((a: number, b: number) => a + b), [2, 3]) //=> 5