Methods
(static) arity(fn) → {Number}
- Source:
- Since:
- 0.1.0
A function that returns number of arguments a function takes.
Example
const sum = (a, b) => a + b;
arity(sum); // => 2
Parameters:
Name | Type | Description |
---|---|---|
fn |
function |
Returns:
Number of arguments a function takes.
- Type
- Number
(static) compose() → {function}
- Source:
- Since:
- 0.1.0
- See:
Function composition is the process of combining two or more functions to produce new function.
Composed function take data and process it through all pipes from right to left producing new data. (f o g) => f(g(o));
Example
const greet = name => `Hello ${name}`;
const greetName = compose(greet, string_capitalize);
greetName('stefan'); // => Hello Stefan
Parameters:
Type | Description |
---|---|
Functions | List of functions to compose. |
Returns:
- Function that expect a value to compute.
- Type
- function
(static) curry(fn) → {function}
- Source:
- Since:
- 0.1.0
- See:
Currying refers to the process of transforming a function with multiple arity into the same function with less arity.
The curried effect is achieved by binding some of the arguments to the first function invoke, so that those values are fixed for the next invocation.
Example
const sum = (a, b, c) => a + b + c;
const sumBy5 = curry(sum, 5);
const sumBy8 = curry(sum)(2)(6);
sumBy5(3, 5); // => 13
sumBy8(6); // => 14
Parameters:
Name | Type | Description |
---|---|---|
fn |
function | The function to curry. |
Throws:
Returns:
Curried function.
- Type
- function
(static) identity(value) → {*}
- Source:
- Since:
- 0.1.0
Function that takes one argument and does nothing but return the value untouched.
Example
identity(10); // => 10
identity(undefined); // => undefined
Parameters:
Name | Type | Description |
---|---|---|
value |
* | Value to be returned. |
Returns:
Returns passed value.
- Type
- *
(static) memoize(fn) → {function}
- Source:
- Since:
- 0.1.0
Creates a function that memoizes the result of function.
Example
const fibonacci = memoize(function(n) {
return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
});
fibonacci(9); // => 34
fibonacci(9); // => From cache: 34
Parameters:
Name | Type | Description |
---|---|---|
fn |
function | Function to memoize. |
Returns:
Memoized function.
- Type
- function
(static) once(fn) → {function}
- Source:
- Since:
- 0.1.0
Creates a version of the function that can only be called one time.
Example
function returnMyName() {
return "Stefan Lazarevic";
}
const callMyNameOnce = once(returnMyName);
callMyNameOnce() // => "Stefan Lazarevic"
callMyNameOnce() // => undefined
Parameters:
Name | Type | Description |
---|---|---|
fn |
function |
Returns:
- Type
- function
(static) partial(fn, args) → {function}
- Source:
- Since:
- 0.1.0
The process of applying a function to some of its arguments. The partially applied function gets returned for later * use. In other words, a function that takes a function with multiple parameters and returns a function with fewer parameters.
Example
const multiply = (a, b) => a * b;
const double = partial(multiply, 2);
double(5); // => 10
Parameters:
Name | Type | Description |
---|---|---|
fn |
function | |
args |
Arguments | arguments to apply. |
Returns:
Function that takes rest of the arguments.
- Type
- function
(static) pipe() → {function}
- Source:
- Since:
- 0.1.0
- See:
Pipe perform function composition like compose.
The only difference between pipe and compose if that pipe takes function arguments from left to right, and by that provides additional readability.
Example
const greet = name => `Hello ${name}`;
const greetName = compose(string_capitalize, greet);
greetName('stefan'); // => Hello Stefan
Parameters:
Type | Description |
---|---|
Functions | List of functions to compose. |
Returns:
- Function that expect a value to compute.
- Type
- function
(static) unary(fn) → {function}
- Source:
- Since:
- 0.1.0
Wraps a function call of any arity to ensure that only one argument is accepted.
At first sign unary function seems useless, but here's the pitfall example that can be prevented using unary function.
Examples
// Failing example without using unary function.
const strNumbers = ["1", "2", "3"];
const numbers = strNumbers.map(parseInt);
console.log(numbers); // => [1, NaN, NaN]
// Passing example using unary function.
const strNumbers = ["1", "2", "3"];
const numbers = strNumbers.map(unary(parseInt));
console.log(numbers); // => [1, 2, 3]
Parameters:
Name | Type | Description |
---|---|---|
fn |
function | The function to wrap. |
Throws:
Returns:
A new function wrapping fn
. The new function is guaranteed to be of arity 1.
- Type
- function