Methods
(static) compose() → {function}
The compose function combines multiple functions from right to left
- Source:
Parameters:
Name | Type | Description |
---|---|---|
...f |
function
|
The function to compose |
Returns:
- Type:
-
function
The result of the composition
Example
const {compose} = require('futils').lambda;
const inc = (n) => n + 1;
const double = (n) => n * 2;
const fc = compose(double, inc);
double(inc(1)) === fc(1); // -> true
(static) constant(a) → {function}
The constant function
- Source:
Parameters:
Name | Type | Description |
---|---|---|
a |
any
|
Any value |
Returns:
- Type:
-
function
A function that ignores its arguments and returns the initial one
Example
const {constant} = require('futils').lambda;
constant(1); // -> (_ -> 1)
constant(null); // -> (_ -> null)
(static) converge(f, gs) → {function}
The converge function
- Since:
- 3.1.2
- Source:
Parameters:
Name | Type | Description |
---|---|---|
f |
function
|
The function to converge into |
gs |
Array
|
An array of branching functions |
Returns:
- Type:
-
function
A function which applies the results of the branching functions to f
Example
const {converge} = require('futils').lambda;
const {prop} = require('futils').operation;
const Person = {
firstName: 'John',
lastName: 'Doe'
};
const greet = (first, last) => `Hello ${first} ${last}`;
const greetPerson = converge(greet, [prop('firstName'), prop('lastName')]);
greetPerson(Person); // -> 'Hello John Doe'
(static) curry(f) → {function}
The curry function. Takes a function and returns a variant of it which takes arguments until enough arguments to execute the given function are consumed. Curry can be used to turn a function which takes multiple arguments at once into a function which takes its arguments in multiple steps
- Source:
Parameters:
Name | Type | Description |
---|---|---|
f |
function
|
The function to curry |
Returns:
- Type:
-
function
The curried variant
Example
const {curry} = require('futils').lambda;
const add = (a, b) => a + b;
const cAdd = curry(add);
add(1); // -> NaN
cAdd(1); // -> (n -> 1 + n)
cAdd(1)(2); // -> 3
(static) curryRight(f) → {function}
The curryRight function. Takes a function and returns a variant of it which takes arguments until enough arguments to execute the given function are consumed. It can be used to turn a function which takes multiple arguments at once into a function which takes its arguments in multiple steps
- Source:
Parameters:
Name | Type | Description |
---|---|---|
f |
function
|
The function to curryRight |
Returns:
- Type:
-
function
The curried variant
Example
const {curryRight} = require('futils').lambda;
const add = (a, b) => a + b;
const cAdd = curryRight(add);
add(1); // -> NaN
cAdd(1); // -> (n -> n + 1)
cAdd(1)(2); // -> 3
(static) fixed(f) → {function}
The fixed function. For a given function f, returns a function g which represents the fixed point of f. Note: The returned function isn't stack safe. If you need stack safety, use the functions in the trampoline package
- Source:
Parameters:
Name | Type | Description |
---|---|---|
f |
function
|
the initial function |
Returns:
- Type:
-
function
The fixed point of f
Example
const {fixed} = require('futils').lambda;
const factorial = fixed((fact) => (n) => n < 1 ? 1 : n * fact(n - 1));
factorial(6); // -> 720
(static) flip(f) → {function}
The flip function flips the order of the first two arguments to a function
- Source:
Parameters:
Name | Type | Description |
---|---|---|
f |
function
|
The function to flip arguments |
Returns:
- Type:
-
function
A variant of f
Example
const {flip} = require('futils').lambda;
const ordered = (a, b, c) => `${a}${b}${c}`;
const flipped = flip(ordered);
ordered(1, 2, 3); // -> '123'
flipped(1, 2, 3); // -> '213'
(static) id(a) → {any}
The identity function
- Source:
Parameters:
Name | Type | Description |
---|---|---|
a |
any
|
Any value |
Returns:
- Type:
-
any
The given value
Example
const {id} = require('futils').lambda;
id(1); // -> 1
id(null); // -> null
id(id); // -> (a -> a)
(static) mcompose() → {function}
The mcompose function combines multiple monad returning functions from right to left
- Since:
- 3.1.3
- Source:
Parameters:
Name | Type | Description |
---|---|---|
...f |
function
|
The functions to compose |
Returns:
- Type:
-
function
The result of the composition
Example
const {mcompose} = require('futils').lambda;
const {Maybe} = require('futils').data;
const {Some, None} = Maybe;
const isNumber = (n) => typeof n === 'number' && !isNaN(n);
const inc = (n) => isNumber(n) ? Some(n + 1) : None();
const double = (n) => isNumber(n) ? Some(n * 2) : None();
const fc = mcompose(double, inc);
fc(1); // -> Some(4)
fc(NaN); // -> None
(static) memoize(f) → {function}
The memoize function, memoizes the result a certain set of arguments gave and returns it immediatly for the same arguments without computation. Memoize can be used to wrap pure functions which do complex calculations to reduce the amount of computations needed on subsequent calls
- Source:
Parameters:
Name | Type | Description |
---|---|---|
f |
function
|
The function to implement meoization |
Returns:
- Type:
-
function
A memoized variant of f
Example
const {memoize} = require('futils').lambda;
const compute = (a, b) => {
console.log(`${a} + ${b}`); // side effect for demo purposes
return a + b;
}
const memCompute = memoize(compute);
compute(1, 2); // -> 3, logs '1 + 2'
compute(1, 2); // -> 3, logs '1 + 2'
memCompute(1, 2); // -> 3, logs '1 + 2'
memCompute(1, 2); // -> 3
(static) mpipe() → {function}
The mpipe function combines multiple monad returning functions from left to right
- Since:
- 3.1.3
- Source:
Parameters:
Name | Type | Description |
---|---|---|
...f |
function
|
The function to pipe |
Returns:
- Type:
-
function
The result of the composition
Example
const {mpipe} = require('futils').lambda;
const {Maybe} = require('futils').data;
const {Some, None} = Maybe;
const isNumber = (n) => typeof n === 'number' && !isNaN(n);
const inc = (n) => isNumber(n) ? Some(n + 1) : None();
const double = (n) => isNumber(n) ? Some(n * 2) : None();
const fc = mpipe(inc, double);
fc(1); // -> Some(4)
fc(NaN); // -> None
(static) not(f) → {function}
The not function. It takes a function and returns a variant of it that negates the return value
- Source:
Parameters:
Name | Type | Description |
---|---|---|
f |
function
|
The function to negate the return value |
Returns:
- Type:
-
function
A variant of f
Example
const {not} = require('futils').lambda;
const isNull = (a) => a === null;
const isntNull = not(isNull);
isNull(null); // -> true
isntNull(null); // -> false
(static) partial(f) → {function}
The partial function allows to implement partial application. Use undefined as placeholder for arguments that should be skipped
- Source:
Parameters:
Name | Type | Description |
---|---|---|
f |
function
|
The function to prepare for partial application |
...as |
any
|
Partial arguments to the function |
Returns:
- Type:
-
function
A partial variant of f
Example
const {partial} = require('futils').lambda;
const greet = (greeting, separator, name) => {
return `${greeting}${separator}${name}`;
}
const pGreet = partial(greet);
greet('Hello', ', ', 'world'); // -> 'Hello, world'
greet('Hello', ', '); // -> 'Hello, undefined'
pGreet('Hello', ', ', 'world'); // -> 'Hello, world'
pGreet('Hello', ', '); // -> (a -> 'Hello, ${a}')
pGreet(undefined, undefined, 'world'); // -> (a -> b -> '${a}${b}world')
(static) partialRight(f) → {function}
The partialRight function allows to implement partial application. Use undefined as placeholder for arguments that should be skipped
- Source:
Parameters:
Name | Type | Description |
---|---|---|
f |
function
|
The function to prepare for partial application |
...as |
any
|
Partial arguments to the function |
Returns:
- Type:
-
function
A partial variant of f
Example
const {partialRight} = require('futils').lambda;
const greet = (greeting, separator, name) => {
return `${greeting}${separator}${name}`;
}
const pGreet = partialRight(greet);
greet('Hello', ', ', 'world'); // -> 'Hello, world'
greet('Hello', ', '); // -> 'Hello, undefined'
pGreet('Hello', ', ', 'world'); // -> 'world, Hello'
pGreet('Hello', ', '); // -> (a -> '${a}, Hello')
pGreet(undefined, undefined, 'world'); // -> (a -> b -> 'world${b}${a}')
(static) pipe() → {function}
The pipe function combines multiple functions from left to right
- Source:
Parameters:
Name | Type | Description |
---|---|---|
...f |
function
|
The function to pipe |
Returns:
- Type:
-
function
The result of the composition
Example
const {pipe} = require('futils').lambda;
const inc = (n) => n + 1;
const double = (n) => n * 2;
const fc = pipe(double, inc);
inc(double(1)) === fc(1); // -> true
(static) upon(f, g) → {any}
The upon function takes two functions and returns the result of calling the first one with the second. This is much similar to a map operation.
- Since:
- 3.1.0
- Source:
Parameters:
Name | Type | Description |
---|---|---|
f |
function
|
The function to operate upon |
g |
function
|
The operating function |
Returns:
- Type:
-
any
The result of g applied to f
Example
const {upon} = require('futils').lambda;
const safe = f => n => typeof n !== 'number' || isNaN(n) ? 0 : f(n);
const double = n => n * 2;
const safeDouble = upon(double, safe);
safeDouble(2); // -> 4
safeDouble(null); // -> 0