[Curried function]

Creates a function that is bound to a context. Note: bind does not provide the additional argument-binding capabilities of Function.prototype.bind.

bind(fn: Function, thisObj: Object): Function
Parameters
fn (Function) The function to bind to context
thisObj (Object) The context to bind fn to
Returns
Function: A function that will execute in the context of thisObj .
Example
const log = bind(console.log, console);
pipe(inc, log, double)(2); //=> 6
// logs: 3

Takes a function f and returns a function g such that:

  • applying g to zero or more arguments will give true if applying the same arguments to f gives a logical false value; and

  • applying g to zero or more arguments will give false if applying the same arguments to f gives a logical true value.

complement(fn: Function): Function
Parameters
fn (Function)
Returns
Function:
Example
const isEven = (n) => n % 2 === 0;
const isOdd = complement(isEven);

isOdd(21); //=> true
isOdd(42); //=> false

Performs right-to-left function composition. The rightmost function may have any arity; the remaining functions must be unary.

compose(functions: ...Function): Function
Parameters
functions (...Function)
Returns
Function: a function that represents the composition of the arguments
Related
pipe
Example
const inc = x => x + 1:
const negate = x => -x;

var f = compose(inc, negate, Math.pow);
f(3, 4); // -(3^4) + 1

Performs right-to-left function composition. The rightmost function may have any arity; the remaining functions must be unary. If any function returns a promise, it chains remaining ones

composeAsync(functions: ...Function): Function
Parameters
functions (...Function)
Returns
Function: a function that represents the composition of the arguments
Related
pipeAsync
Example
var head = ([h]) => h;
var process = (a) => Promise.resolve(a);
var promiseAll = Promise.all.bind(Promise);

var f = composeAsync(head, promiseAll, process);

await f([1, 2]); // Promise.all([process(1), process(2)])[0] => 1

Returns a curried equivalent of the provided function. The curried function has two unusual capabilities. First, its arguments needn't be provided one at a time. If f is a ternary function and g is curry(f), the following are equivalent:

  • g(1)(2)(3)
  • g(1)(2, 3)
  • g(1, 2)(3)
  • g(1, 2, 3)
curry(fn: Function, initialArgs: ...any): Function
Parameters
fn (Function) The function to curry.
initialArgs (...any) The fn initial arguments.
Returns
Function: A new, curried function.
Related
curryN
Example
var addFourNumbers = function(a, b, c, d) {
  return a + b + c + d;
};
var curriedAddFourNumbers = curry(addFourNumbers);
var f = curriedAddFourNumbers(1, 2);
var g = f(3);
g(4); //=> 10

Returns a curried equivalent of the provided function, with the specified arity. The curried function has two unusual capabilities. First, its arguments needn't be provided one at a time. If g is curryN(3, f), the following are equivalent:

  • g(1)(2)(3)
  • g(1)(2, 3)
  • g(1, 2)(3)
  • g(1, 2, 3)
curryN(arity: Number, fn: Function, initialArgs: ...any): Function
Parameters
arity (Number) The arity for the returned function.
fn (Function) The function to curry.
initialArgs (...any) The function initial arguments.
Returns
Function: A new, curried function.
Related
curry
Example
var addFourNumbers = function(a, b, c, d = 0) {
  return a + b + c + d;
};

var curriedAddFourNumbers = curryN(3, addFourNumbers);
var f = curriedAddFourNumbers(1, 2);
f(3); //=> 6

[Curried function]

Returns a new list containing only those items that match a given predicate function. The predicate function is passed: (value, index, collection). in the case of filtering and object, the predicate function is passed: (value, key, collection).

Note that filter does not skip deleted or unassigned indices, unlike the native Array.prototype.filter method. For more details on this behavior, see: Array.prototype.filter

Acts as a transducer if a transformer is given as second parameter.

filter(fn: Function, collection: Collection?): (Collection | Function)
Parameters
fn (Function) The function called per iteration.
collection (Collection?) The collection to iterate over.
Returns
(Collection | Function): The new filtered array.
Related
transduce
Example
const isEven = (n) => n % 2 === 0;
const isValueEven = ([k, v]) => isEven(v)
const isVowel = (c) => /[aeiou]/.test(c);

filter(isVowel, 'string'); //=> 'i'
filter(isEven, [1, 2, 3]); //=> [2]
filter(isEven, Set[1, 2, 3]); //=> Set[2]
filter(isEven, function* () { yield* [1, 2, 3]}); //=> [2]
filter(isEven, { a: 1, b: 2 }); //=> { b: 2 }
filter(isValueEven, Map{ a: 1, b: 2 }); //=> Map{ b: 2 }

see The Transducer Protocol

Transducer

Type: Object

Properties
null (Function) : @@transducer/init - Function: () => any, Hook called at the beginning to compute the initial state
null (Function) : @@transducer/result - Function: (state) => any, Hook called when the result is computed
null (Function) : @@transducer/step - Function: (state, value, index, collection) => any, Hook called at every iteration
Collection

Type: (Array | Object | Transducer | Set | Map | string | Iterator | GeneratorFunction | Generator | Iterable)

Into

Type: Object

Properties
array (function (Function, Collection): Array?) : Function: (value, index, collection, accumulator) => any, Merge strategy: Array.push
string (function (Function, Collection): string?) : Function: (value, index, collection, accumulator) => any, Merge strategy: concatenation
number (function (Function, Collection): number?) : Function: (value, index, collection, accumulator) => any, Merge strategy: last returned value becomes the accumulator
set (function (Function, Collection): Set?) : Function: (value, index, collection, accumulator) => any, Merge strategy: Set.add
map (function (Function, Collection): Map?) : Function: ([key, value], index, collection, accumulator) => [any, any], Merge strategy: Map.add
object (function (Function, Collection): Object?) : Function: ([key, value], index, collection, accumulator) => [any, any], Merge strategy: object assignation

Transforms the items of the iterable with the transducer and appends the transformed items to the accumulator using an appropriate iterator function based on the accumulator type.

The transducer function takes 4 params: (value, index, collection, accumulator). Then value parameter is a pair [key, value] when the accumulator is an Object or a Map.

In the case the accumulator is a number (into.number), there is no merge strategy and just returns what the transducer returns

The iteration is performed with reduce after initializing the transducer.

All properties are curried.

into

Type: Into

Example
var value = ([k, v]] => v
var isEven = (x) => x % 2 === 0
var isVowel = (x) => /aeiou/.test(x)
var isObject = (x) => typeof x === 'object'
var toUppercase = (x) => x.toUppercase()
var addAccumulator = (value, index, collection, accumulator) => value + accumulator
var pairById = (x) => [x.id, x]

into.array(compose(map(value), filter(isEven)), {a: 1, b: 2}) // [2]

into.string(compose(filter(isVowel), map(toUppercase)), 'string') // 'I'

into.number(map(addAccumulator), [1, 2, 3]) // 1 + 2 + 3 = 6

into.object(
  compose(filter(isObject), map(pairById)),
    [1, { id: '1-2-3', value: 10 }]
  ) // { '1-2-3': { id: '1-2-3', value: 10 } }

[Curried function]

See if an object (val) is an instance of the supplied constructor. This function will check up the inheritance chain, if any.

  • Additionally, it has the following methods for nicer and more semantic type checking: array - function - functor - filterable - generator - iterator - map - object (strict checking) - promise - set - string - thenable - transducer - type - strict - nil - not (negates any of the previous ones)
  • @func
is
Parameters
ctor (Object) A constructor
val (any) The value to test
Properties
array (function (any): boolean?) : Checks if is an Array
function (function (any): boolean?) : Checks if is type function
functor (function (any): boolean?) : Checks if it has a map method
filterable (function (any): boolean?) : Checks if it has a filter method
generator (function (any): boolean?) : Checks if it is a generator function
iterator (function (any): boolean?) : Checks if it has [Symbol.iterator] method
map (function (any): boolean?) : Checks if it is a Map or an instance of a Map
object (function (any): boolean?) : Checks if the constructor equals to Object (if it is strictly an object)
promise (function (any): boolean?) : Checks if it is a Promise or an instance of a Promise
set (function (any): boolean?) : Checks if it is a Set or an instance of a Set
string (function (any): boolean?) : Checks if it is boolean type
thenable (function (any): boolean?) : Checks if it has a then method
transducer (function (any): boolean?) : Checks if it is an object that follows the transducer protocol
type (function (string, any): boolean?) : Checks if the type matches
strict (function (Function, any): boolean?) : Checks if the constructor function matches
nil (function (any): boolean?) : Checks if it is null or undefined
not (Object?) : negates the following method
  • not.array function (any): boolean?

    Checks if is not an Array

  • not.function function (any): boolean?

    Checks if is not type function

  • not.functor function (any): boolean?

    Checks if it has not a map method

  • not.filterable function (any): boolean?

    Checks if it has not a filter method

  • not.generator function (any): boolean?

    Checks if it is not a generator function

  • not.iterator function (any): boolean?

    Checks if it has not [Symbol.iterator] method

  • not.map function (any): boolean?

    Checks if it is not a Map nor an instance of a Map

  • not.object function (any): boolean?

    Checks if the constructor not equals to Object (if it is strictly not an object)

  • not.promise function (any): boolean?

    Checks if it is not a Promise nor an instance of a Promise

  • not.set function (any): boolean?

    Checks if it is not a Set nor an instance of a Set

  • not.string function (any): boolean?

    Checks if it is not boolean type

  • not.thenable function (any): boolean?

    Checks if it has not a then method

  • not.transducer function (any): boolean?

    Checks if it is not an object that follows the transducer protocol

  • not.type function (string, any): boolean?

    Checks if the type do not matches matches

  • not.strict function (Function, any): boolean?

    Checks if the constructor function do not matches

  • not.nil function (any): boolean?

    Checks if it is not null or undefined

Returns
Boolean:
Example
is.object([]); => false
is(Object, []); //=> true
is(Object, {}); //=> true
is(Number, 1); //=> true
is(Object, 1); //=> false
is(String, 's'); //=> true
is(String, new String('')); //=> true
is(Object, new String('')); //=> true
is(Object, 's'); //=> false
is(Number, {}); //=> false
is.function(() => {}); => true
is.not.function(() => {}); => false

[Curried function]

Returns a new Iterable, constructed by applying the supplied function to every element of the supplied list.

When mapping an object, the function mapper accepts (value, key, object) In any other case, the mapper function accepts (value, index, collection). The value comes from the iterator of the collection.

Note: map does not skip deleted or unassigned indices (sparse arrays), unlike the native Array.prototype.map method. For more details on this behavior, see: Array.prototype.map

Acts as a transducer if a transformer is given as second parameter.

map(fn: Function, collection: Collection?): (Collection | Function)
Parameters
fn (Function) The function to be called on every element of the input Iterable .
collection (Collection?) The Iterable to be iterated over.
Returns
(Collection | Function): The new Iterable or curried function.
Related
transduce
Example
const toUpperCase = (x) => x.toUpperCase();
const double = (x) => x * 2;
const doubleValue = ([k, v]) => [k, v * 2];

map(toUpperCase, 'aeiou'); //=> 'AEIOU'
map(double, [1, 2, 3]); //=> [2, 4, 6]
map(double, Set[1, 2, 3]); //=> Set[2, 4, 6]
map(double, function* () { yield* [1, 2, 3]}); //=> [2, 4, 6]
map(double, { a: 1, b: 2 }); //=> { a: 2, b: 4 }
map(doubleValue, Map{ a: 1, b: 2 }); //=> Map{ a: 2, b: 4 }

Performs left-to-right function composition. The leftmost function may have any arity; the remaining functions must be unary.

In some libraries this function is named sequence.

pipe(functions: ...Function): Function
Parameters
functions (...Function)
Returns
Function: a function that represents the composition of the arguments
Related
compose
Example
var f = pipe(Math.pow, toString);

f(3, 4); // (3^4).toString()

Performs left-to-right function composition. The leftmost function may have any arity; the remaining functions must be unary. If any function returns a promise, it chains remaining ones

pipeAsync(functions: ...Function): Function
Parameters
functions (...Function)
Returns
Function: a function that represents the composition of the arguments
Related
composeAsync
Example
var head = ([h]) => h;
var process = (a) => Promise.resolve(a);
var promiseAll = Promise.all.bind(Promise);

var f = pipeAsync(process, promiseAll, head);

f([1, 2]); // Promise.all([process(1), process(2)])[0] => 1

[Curried function]

Returns a single item by iterating through the collection, successively calling the iterator function and passing it an accumulator value and the current value from the array, and then passing the result to the next call.

The iterator function receives: (acc, value, index, collection). while reducing an object it receives: (acc, [value, key], collection). It may use reduced to stop the iteration.

It also accepts a transducer instead of an iterator function.

Note: reduce does not skip deleted or unassigned indices (sparse arrays), unlike the native Array.prototype.reduce method. For more details on this behavior, see: Array.prototype.reduce

reduce(transducer: Function, initial: any, collection: Collection): any
Parameters
transducer (Function) The iterator function. Receives four values: the accumulator, the current element from the Iterable, the iteration index and the whole Iterable. It also accepts a Transducer
initial (any) The accumulator initial value.
collection (Collection) An Iterable parameter.
Returns
any: The final, accumulated value.
Related
reduced
Example
const add = (acc, act) => a + b;

reduce(add, 'normal', 'string'); //=> 'normal string'
reduce(add, 10, [1, 2, 3]); //=> 16
reduce(add, 10, Set[1, 2, 3]); //=> 16
reduce(add, 10, function* () { yield* [1, 2, 3]}); //=> 16
reduce(add, '', { a: 1, b: 2 }); //=> 'a,1b,2'
reduce(add, '', Map{ a: 1, b: 2 }); //=> 'a,1b,2'

Returns a value wrapped to indicate that it is the final value of the reduce and transduce functions. The returned value should be considered a black box: the internal structure is not guaranteed to be stable.

reduced(state: any): any
Parameters
state (any) The final value of the reduce.
Returns
any: The wrapped value.
Related
reduce, transduce
Example
reduce(
  pipe(add, ifElse(lte(10), reduced, identity)),
  0,
  [1, 2, 3, 4, 5]
) // 10

[Curried function]

Returns the first n elements of the iterable.

take(n: Number, collection: (Array | string | Iterator | GeneratorFunction | Generator)): any
Parameters
n (Number)
collection ((Array | string | Iterator | GeneratorFunction | Generator))
Returns
any:
Example
const names = names

take(1, names); //=> ['foo']
take(1, function* () { yield* names }); //=> ['foo']
take(1, names); //=> ['foo']
take(2, names); //=> ['foo', 'bar']
take(3, names); //=> ['foo', 'bar', 'baz']
take(4, names); //=> ['foo', 'bar', 'baz']
take(3, 'ramda'); //=> 'ram'
take(1, Map{ a: 1, b: 2 }) => [['a', 1]]

const personnel = [
 'Dave Brubeck',
 'Paul Desmond',
 'Eugene Wright',
 'Joe Morello',
 'Gerry Mulligan',
 'Bob Bates',
 'Joe Dodge',
 'Ron Crotty'
];

const takeTwo = take(2);
takeFive(personnel); //=> ['Dave Brubeck', 'Paul Desmond']

[Curried function]

Runs the given function with the supplied object, then returns the object.

Acts as a transducer if a transformer is given as second parameter.

tap(fn: Function, x: any): any
Parameters
fn (Function) The function to call with x . The return value of fn will be thrown away.
x (any)
Returns
any: x .
Example
const sayX = x => console.log('x is ' + x);
tap(sayX, 100); //=> 100
// logs 'x is 100'

[Curried function]

Initializes a transducer using supplied iterator function. Returns a single item by iterating through the list, successively calling the transformed iterator function and passing it an accumulator value and the current value from the array, and then passing the result to the next call.

The iterator function receives two values: (acc, value). It will be wrapped as a transformer to initialize the transducer. A transformer can be passed directly in place of an iterator function. In both cases, iteration may be stopped early with the reduced function.

A transducer is a function that accepts a transformer and returns a transformer and can be composed directly.

A transformer is an object that provides a 2-arity reducing iterator function, step, 0-arity initial value function, init, and 1-arity result extraction function, result. The step function is used as the iterator function in reduce. The result function is used to convert the final accumulator into the return type and in most cases is the identity function. The init function can be used to provide an initial accumulator, but is ignored by transduce.

The iteration is performed with reduce after initializing the transducer.

transduce(transducer: Function, combine: Function, initial: any, collection: Collection): any
Parameters
transducer (Function) The transducer function. Receives a transformer and returns a transformer.
combine (Function) The iterator function. Receives two values, the accumulator and the current element from the array. Wrapped as transformer, if necessary, and used to initialize the transducer
initial (any) The initial accumulator value.
collection (Collection) The Iterable to iterate over.
Returns
any: The final, accumulated value.
Related
reduce, reduced, into
Example
var t = transducers;
var inc = function(n) { return n+1; };
var isEven = function(n) { return n % 2 == 0; };
var apush = function(arr,x) { arr.push(x); return arr; };
var xf = compose(map(inc),filter(isEven));
transduce(xf, apush, [], [1,2,3,4]); // [2,4]