A practical functional library for data transformations in JavaScript.
Take a look at the interactive documentation
To use with node:
$ npm install @hamlab/data-transformations
Then in the console:
const DT = require('@hamlab/data-transformations');
const { map } = require('@hamlab/data-transformations/map');
...
- bind
- complement
- compose
- composeAsync
- curry
- curryN
- filter
- Transducer
- Collection
- Into
- into
- is
- map
- pipe
- pipeAsync
- reduce
- reduced
- take
- tap
- transduce
[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.
const log = bind(console.log, console);
pipe(inc, log, double)(2); //=> 6
// logs: 3
Returns Function A function that will execute in the context of thisObj
.
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 tof
gives a logical false value; and -
applying
g
to zero or more arguments will give false if applying the same arguments tof
gives a logical true value.
fn
Function
const isEven = (n) => n % 2 === 0;
const isOdd = complement(isEven);
isOdd(21); //=> true
isOdd(42); //=> false
Returns Function
- See: pipe
Performs right-to-left function composition. The rightmost function may have any arity; the remaining functions must be unary.
functions
...Function
const inc = x => x + 1:
const negate = x => -x;
var f = compose(inc, negate, Math.pow);
f(3, 4); // -(3^4) + 1
Returns Function a function that represents the composition of the arguments
- See: pipeAsync
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
functions
...Function
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 Function a function that represents the composition of the arguments
- See: curryN
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)
fn
Function The function to curry.initialArgs
...any The fn initial arguments.
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 Function A new, curried function.
- See: curry
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)
arity
Number The arity for the returned function.fn
Function The function to curry.initialArgs
...any The function initial arguments.
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
Returns Function A new, curried function.
- See: transduce
[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.
fn
Function The function called per iteration.collection
Collection? The collection to iterate over.
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 }
Returns (Collection | Function) The new filtered array.
Type: Object
null
Function@@transducer/init
- Function: () => any, Hook called at the beginning to compute the initial statenull
Function@@transducer/result
- Function: (state) => any, Hook called when the result is computednull
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
array
function (Function, Collection): Array? Function: (value, index, collection, accumulator) => any, Merge strategy: Array.pushstring
function (Function, Collection): string? Function: (value, index, collection, accumulator) => any, Merge strategy: concatenationnumber
function (Function, Collection): number? Function: (value, index, collection, accumulator) => any, Merge strategy: last returned value becomes the accumulatorset
function (Function, Collection): Set? Function: (value, index, collection, accumulator) => any, Merge strategy: Set.addmap
function (Function, Collection): Map? Function: ([key, value], index, collection, accumulator) => [any, any], Merge strategy: Map.addobject
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.
- 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
ctor
Object A constructorval
any The value to test
-
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 methodnot.array
function (any): boolean? Checks if is not an Arraynot.function
function (any): boolean? Checks if is not type functionnot.functor
function (any): boolean? Checks if it has not a map methodnot.filterable
function (any): boolean? Checks if it has not a filter methodnot.generator
function (any): boolean? Checks if it is not a generator functionnot.iterator
function (any): boolean? Checks if it has not [Symbol.iterator] methodnot.map
function (any): boolean? Checks if it is not a Map nor an instance of a Mapnot.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 Promisenot.set
function (any): boolean? Checks if it is not a Set nor an instance of a Setnot.string
function (any): boolean? Checks if it is not boolean typenot.thenable
function (any): boolean? Checks if it has not a then methodnot.transducer
function (any): boolean? Checks if it is not an object that follows the transducer protocolnot.type
function (string, any): boolean? Checks if the type do not matches matchesnot.strict
function (Function, any): boolean? Checks if the constructor function do not matchesnot.nil
function (any): boolean? Checks if it is not null or undefined
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
Returns Boolean
- See: transduce
[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.
fn
Function The function to be called on every element of the inputIterable
.collection
Collection? The Iterable to be iterated over.
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 }
Returns (Collection | Function) The new Iterable or curried function.
- See: compose
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
.
functions
...Function
var f = pipe(Math.pow, toString);
f(3, 4); // (3^4).toString()
Returns Function a function that represents the composition of the arguments
- See: composeAsync
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
functions
...Function
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
Returns Function a function that represents the composition of the arguments
- See: reduced
[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
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 Transducerinitial
any The accumulator initial value.collection
Collection An Iterable parameter.
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 any The final, accumulated value.
- See: reduce, transduce
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.
state
any The final value of the reduce.
reduce(
pipe(add, ifElse(lte(10), reduced, identity)),
0,
[1, 2, 3, 4, 5]
) // 10
Returns any The wrapped value.
[Curried function]
Returns the first n
elements of the iterable.
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']
Returns any
[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.
fn
Function The function to call withx
. The return value offn
will be thrown away.x
any
const sayX = x => console.log('x is ' + x);
tap(sayX, 100); //=> 100
// logs 'x is 100'
Returns any x
.
- See: reduce, reduced, into
[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.
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 transducerinitial
any The initial accumulator value.collection
Collection The Iterable to iterate over.
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]
Returns any The final, accumulated value.
Creates a function that is bound to a context.
Note: bind
does not provide the additional argument-binding capabilities of
Function.prototype.bind.
const log = bind(console.log, console);
pipe(inc, log, double)(2); //=> 6
// logs: 3
Returns Function A function that will execute in the context of thisObj
.
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 tof
gives a logical false value; and -
applying
g
to zero or more arguments will give false if applying the same arguments tof
gives a logical true value.
fn
Function
const isEven = (n) => n % 2 === 0;
const isOdd = complement(isEven);
isOdd(21); //=> true
isOdd(42); //=> false
Returns Function
- See: pipe
Performs right-to-left function composition. The rightmost function may have any arity; the remaining functions must be unary.
functions
...Function
const inc = x => x + 1:
const negate = x => -x;
var f = compose(inc, negate, Math.pow);
f(3, 4); // -(3^4) + 1
Returns Function a function that represents the composition of the arguments
- See: pipeAsync
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
functions
...Function
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 Function a function that represents the composition of the arguments
- See: curryN
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)
fn
Function The function to curry.initialArgs
...any The fn initial arguments.
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 Function A new, curried function.
- See: curry
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)
arity
Number The arity for the returned function.fn
Function The function to curry.initialArgs
...any The function initial arguments.
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
Returns Function A new, curried function.
- See: transduce
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.
fn
Function The function called per iteration.collection
Collection? The collection to iterate over.
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 }
Returns (Collection | Function) The new filtered array.
Type: Object
null
Function@@transducer/init
- Function: () => any, Hook called at the beginning to compute the initial statenull
Function@@transducer/result
- Function: (state) => any, Hook called when the result is computednull
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
array
function (Function, Collection): Array? Function: (value, index, collection, accumulator) => any, Merge strategy: Array.pushstring
function (Function, Collection): string? Function: (value, index, collection, accumulator) => any, Merge strategy: concatenationnumber
function (Function, Collection): number? Function: (value, index, collection, accumulator) => any, Merge strategy: last returned value becomes the accumulatorset
function (Function, Collection): Set? Function: (value, index, collection, accumulator) => any, Merge strategy: Set.addmap
function (Function, Collection): Map? Function: ([key, value], index, collection, accumulator) => [any, any], Merge strategy: Map.addobject
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.
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 } }
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
ctor
Object A constructorval
any The value to test
-
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 methodnot.array
function (any): boolean? Checks if is not an Arraynot.function
function (any): boolean? Checks if is not type functionnot.functor
function (any): boolean? Checks if it has not a map methodnot.filterable
function (any): boolean? Checks if it has not a filter methodnot.generator
function (any): boolean? Checks if it is not a generator functionnot.iterator
function (any): boolean? Checks if it has not [Symbol.iterator] methodnot.map
function (any): boolean? Checks if it is not a Map nor an instance of a Mapnot.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 Promisenot.set
function (any): boolean? Checks if it is not a Set nor an instance of a Setnot.string
function (any): boolean? Checks if it is not boolean typenot.thenable
function (any): boolean? Checks if it has not a then methodnot.transducer
function (any): boolean? Checks if it is not an object that follows the transducer protocolnot.type
function (string, any): boolean? Checks if the type do not matches matchesnot.strict
function (Function, any): boolean? Checks if the constructor function do not matchesnot.nil
function (any): boolean? Checks if it is not null or undefined
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
Returns Boolean
- See: transduce
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.
fn
Function The function to be called on every element of the inputIterable
.collection
Collection? The Iterable to be iterated over.
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 }
Returns (Collection | Function) The new Iterable or curried function.
- See: compose
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
.
functions
...Function
var f = pipe(Math.pow, toString);
f(3, 4); // (3^4).toString()
Returns Function a function that represents the composition of the arguments
- See: composeAsync
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
functions
...Function
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
Returns Function a function that represents the composition of the arguments
- See: reduced
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
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 Transducerinitial
any The accumulator initial value.collection
Collection An Iterable parameter.
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 any The final, accumulated value.
- See: reduce, transduce
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.
state
any The final value of the reduce.
reduce(
pipe(add, ifElse(lte(10), reduced, identity)),
0,
[1, 2, 3, 4, 5]
) // 10
Returns any The wrapped value.
Returns the first n
elements of the iterable.
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']
Returns any
Runs the given function with the supplied object, then returns the object.
Acts as a transducer if a transformer is given as second parameter.
fn
Function The function to call withx
. The return value offn
will be thrown away.x
any
const sayX = x => console.log('x is ' + x);
R.tap(sayX, 100); //=> 100
// logs 'x is 100'
Returns any x
.
- See: reduce, reduced, into
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 R.identity. 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.
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 transducerinitial
any The initial accumulator value.collection
Collection The Iterable to iterate over.
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]
Returns any The final, accumulated value.