1.0.29
[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.
Function
:
A function that will execute in the context of
thisObj
.
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.
(Function)
Function
:
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.
(...Function)
Function
:
a function that represents the composition of the arguments
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
(...Function)
Function
:
a function that represents the composition of the arguments
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)
(Function)
The function to curry.
(...any)
The fn initial arguments.
Function
:
A new, curried function.
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)
(Number)
The arity for the returned function.
(Function)
The function to curry.
(...any)
The function initial arguments.
Function
:
A new, curried function.
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.
(Function)
The function called per iteration.
(Collection?)
The collection to iterate over.
(Collection | Function)
:
The new filtered array.
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 }
Type: Object
(Function)
: @@transducer/init
- Function: () => any, Hook called at the beginning to compute the initial state
(Function)
: @@transducer/result
- Function: (state) => any, Hook called when the result is computed
(Function)
: @@transducer/step
- Function: (state, value, index, collection) => any, Hook called at every iteration
Type: (Array | Object | Transducer | Set | Map | string | Iterator | GeneratorFunction | Generator | Iterable)
Type: Object
(function (Function, Collection): Array?)
: Function: (value, index, collection, accumulator) => any, Merge strategy: Array.push
(function (Function, Collection): string?)
: Function: (value, index, collection, accumulator) => any, Merge strategy: concatenation
(function (Function, Collection): number?)
: Function: (value, index, collection, accumulator) => any, Merge strategy: last returned value becomes the accumulator
(function (Function, Collection): Set?)
: Function: (value, index, collection, accumulator) => any, Merge strategy: Set.add
(function (Function, Collection): Map?)
: Function: ([key, value], index, collection, accumulator) => [any, any], Merge strategy: Map.add
(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.
Type: Into
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.
(Object)
A constructor
(any)
The value to test
(function (any): boolean?)
: Checks if is an Array
(function (any): boolean?)
: Checks if is type function
(function (any): boolean?)
: Checks if it has a map method
(function (any): boolean?)
: Checks if it has a filter method
(function (any): boolean?)
: Checks if it is a generator function
(function (any): boolean?)
: Checks if it has [Symbol.iterator] method
(function (any): boolean?)
: Checks if it is a Map or an instance of a Map
(function (any): boolean?)
: Checks if the constructor equals to Object (if it is strictly an object)
(function (any): boolean?)
: Checks if it is a Promise or an instance of a Promise
(function (any): boolean?)
: Checks if it is a Set or an instance of a Set
(function (any): boolean?)
: Checks if it is boolean type
(function (any): boolean?)
: Checks if it has a then method
(function (any): boolean?)
: Checks if it is an object that follows the transducer protocol
(function (any): boolean?)
: Checks if it is null or undefined
(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
Boolean
:
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.
(Collection?)
The Iterable to be iterated over.
(Collection | Function)
:
The new Iterable or curried function.
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
.
(...Function)
Function
:
a function that represents the composition of the arguments
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
(...Function)
Function
:
a function that represents the composition of the arguments
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
(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
(any)
The accumulator initial value.
(Collection)
An Iterable parameter.
any
:
The final, accumulated value.
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.
(any)
The final value of the reduce.
any
:
The wrapped value.
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.
any
:
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.
(any)
any
:
x
.
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.
(Function)
The transducer function. Receives a transformer and returns a transformer.
(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
(any)
The initial accumulator value.
(Collection)
The Iterable to iterate over.
any
:
The final, accumulated value.
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]