Ramda
__ Added in v0.6.0
A special placeholder value used to specify "gaps" within curried functions, allowing partial application of any combination of arguments, regardless of their positions.
If g is a curried ternary function and _ is R.__, the following are equivalent:
g(1, 2, 3)g(_, 2, 3)(1)g(_, _, 3)(1)(2)g(_, _, 3)(1, 2)g(_, 2, _)(1, 3)g(_, 2)(1)(3)g(_, 2)(1, 3)g(_, 2)(_, 3)(1)
const greet = R.replace('{name}', R.__, 'Hello, {name}!');
greet('Alice'); //=> 'Hello, Alice!' add Added in v0.1.0
Number → Number → Number
Parameters
-
a -
b
Returns
- Number
Adds two values.
See also subtract.
R.add(2, 3); //=> 5 R.add(7)(10); //=> 17
addIndex Added in v0.15.0
(((a …) → b) … → [a] → *) → (((a …, Int, [a]) → b) … → [a] → *)
Parameters
-
fnA list iteration function that does not pass index or list to its callback
Returns
- function An altered list iteration function that passes (item, index, list) to its callback
Creates a new list iteration function from an existing one by adding two new parameters to its callback function: the current index, and the entire list.
This would turn, for instance, R.map function into one that more closely resembles Array.prototype.map. Note that this will only work for functions in which the iteration callback function is the first parameter, and where the list is the last parameter. (This latter might be unimportant if the list parameter is not used.)
const mapIndexed = R.addIndex(R.map); mapIndexed((val, idx) => idx + '-' + val, ['f', 'o', 'o', 'b', 'a', 'r']); //=> ['0-f', '1-o', '2-o', '3-b', '4-a', '5-r']
addIndexRight Added in v0.29.0
((a … → b) … → [a] → *) → (a …, Int, [a] → b) … → [a] → *)
Parameters
-
fnA list iteration function that does not pass index or list to its callback
Returns
- function An altered list iteration function that passes (item, index, list) to its callback
As with addIndex, addIndexRight creates a new list iteration function from an existing one by adding two new parameters to its callback function: the current index, and the entire list.
Unlike addIndex, addIndexRight iterates from the right to the left.
const revmap = (fn, ary) => R.map(fn, R.reverse(ary)); const revmapIndexed = R.addIndexRight(revmap); revmapIndexed((val, idx) => idx + '-' + val, ['f', 'o', 'o', 'b', 'a', 'r']); //=> [ '5-r', '4-a', '3-b', '2-o', '1-o', '0-f' ]
adjust Added in v0.14.0
Number → (a → a) → [a] → [a]
Parameters
-
idxThe index.
-
fnThe function to apply.
-
listAn array-like object whose value at the supplied index will be replaced.
Returns
- Array A copy of the supplied array-like object with the element at index `idx` replaced with the value returned by applying `fn` to the existing element.
Applies a function to the value at the given index of an array, returning a new copy of the array with the element at the given index replaced with the result of the function application.
When idx < -list.length || idx >= list.length, the original list is returned.
See also update.
R.adjust(1, R.toUpper, ['a', 'b', 'c', 'd']); //=> ['a', 'B', 'c', 'd'] R.adjust(-1, R.toUpper, ['a', 'b', 'c', 'd']); //=> ['a', 'b', 'c', 'D'] // out-of-range returns original list R.adjust(4, R.toUpper, ['a', 'b', 'c', 'd']); //=> ['a', 'b', 'c', 'd'] R.adjust(-5, R.toUpper, ['a', 'b', 'c', 'd']); //=> ['a', 'b', 'c', 'd']
all Added in v0.1.0
(a → Boolean) → [a] → Boolean
Parameters
-
fnThe predicate function.
-
listThe array to consider.
Returns
- Boolean `true` if the predicate is satisfied by every element, `false` otherwise.
Returns true if all elements of the list match the predicate, false if there are any that don't.
Dispatches to the all method of the second argument, if present.
Acts as a transducer if a transformer is given in list position.
See also any, none, transduce.
const equals3 = R.equals(3); R.all(equals3)([3, 3, 3, 3]); //=> true R.all(equals3)([3, 3, 1, 3]); //=> false
allPass Added in v0.9.0
[(*… → Boolean)] → (*… → Boolean)
Parameters
-
predicatesAn array of predicates to check
Returns
- function The combined predicate
Takes a list of predicates and returns a predicate that returns true for a given list of arguments if every one of the provided predicates is satisfied by those arguments.
The function returned is a curried function whose arity matches that of the highest-arity predicate.
const isQueen = R.propEq('Q', 'rank');
const isSpade = R.propEq('♠︎', 'suit');
const isQueenOfSpades = R.allPass([isQueen, isSpade]);
isQueenOfSpades({rank: 'Q', suit: '♣︎'}); //=> false
isQueenOfSpades({rank: 'Q', suit: '♠︎'}); //=> true always Added in v0.1.0
a → (* → a)
Parameters
-
valThe value to wrap in a function
Returns
- function A Function :: * -> val.
Returns a function that always returns the given value. Note that for non-primitives the value returned is a reference to the original value.
This function is known as const, constant, or K (for K combinator) in other languages and libraries.
const t = R.always('Tee');
t(); //=> 'Tee' and Added in v0.1.0
a → b → a | b
Parameters
-
a -
b
Returns
- Any
Returns the first argument if it is falsy, otherwise the second argument. Acts as the boolean and statement if both inputs are Booleans.
R.and(true, true); //=> true R.and(true, false); //=> false R.and(false, true); //=> false R.and(false, false); //=> false
andThen Added in v0.27.1
(a → b) → (Promise e a) → (Promise e b)
(a → (Promise e b)) → (Promise e a) → (Promise e b)Parameters
-
onSuccessThe function to apply. Can return a value or a promise of a value.
-
p
Returns
- Promise The result of calling `p.then(onSuccess)`
Returns the result of applying the onSuccess function to the value inside a successfully resolved promise. This is useful for working with promises inside function compositions.
const makeQuery = email => ({ query: { email }});
const fetchMember = request =>
Promise.resolve({ firstName: 'Bob', lastName: 'Loblaw', id: 42 });
const pickName = R.pick(['firstName', 'lastName'])
//getMemberName :: String -> Promise ({ firstName, lastName })
const getMemberName = R.pipe(
makeQuery,
fetchMember,
R.andThen(pickName),
);
// Alternately
const getMemberName = R.pipe(
makeQuery,
fetchMember,
)
R.pipeWith(R.andThen, [getMemberName, pickName])('bob@gmail.com').then(console.log)
// logs {"firstName":"Bob","lastName":"Loblaw"}
getMemberName('bob@gmail.com').then(console.log); any Added in v0.1.0
(a → Boolean) → [a] → Boolean
Parameters
-
fnThe predicate function.
-
listThe array to consider.
Returns
- Boolean `true` if the predicate is satisfied by at least one element, `false` otherwise.
Returns true if at least one of the elements of the list match the predicate, false otherwise.
Dispatches to the any method of the second argument, if present.
Acts as a transducer if a transformer is given in list position.
See also all, none, transduce.
const lessThan0 = R.flip(R.lt)(0); const lessThan2 = R.flip(R.lt)(2); R.any(lessThan0)([1, 2]); //=> false R.any(lessThan2)([1, 2]); //=> true
anyPass Added in v0.9.0
[(*… → Boolean)] → (*… → Boolean)
Parameters
-
predicatesAn array of predicates to check
Returns
- function The combined predicate
Takes a list of predicates and returns a predicate that returns true for a given list of arguments if at least one of the provided predicates is satisfied by those arguments.
The function returned is a curried function whose arity matches that of the highest-arity predicate.
const isClub = R.propEq('♣', 'suit');
const isSpade = R.propEq('♠', 'suit');
const isBlackCard = R.anyPass([isClub, isSpade]);
isBlackCard({rank: '10', suit: '♣'}); //=> true
isBlackCard({rank: 'Q', suit: '♠'}); //=> true
isBlackCard({rank: 'Q', suit: '♦'}); //=> false ap Added in v0.3.0
[a → b] → [a] → [b]
Apply f => f (a → b) → f a → f b(r → a → b) → (r → a) → (r → b)Parameters
-
applyF -
applyX
Returns
- *
ap applies a list of functions to a list of values.
Dispatches to the ap method of the first argument, if present. Also treats curried functions as applicatives.
R.ap([R.multiply(2), R.add(3)], [1,2,3]); //=> [2, 4, 6, 4, 5, 6]
R.ap([R.concat('tasty '), R.toUpper], ['pizza', 'salad']); //=> ["tasty pizza", "tasty salad", "PIZZA", "SALAD"]
// R.ap can also be used as S combinator
// when only two functions are passed
R.ap(R.concat, R.toUpper)('Ramda') //=> 'RamdaRAMDA' aperture Added in v0.12.0
Number → [a] → [[a]]
Parameters
-
nThe size of the tuples to create
-
listThe list to split into
n-length tuples
Returns
- Array The resulting list of `n`-length tuples
Returns a new list, composed of n-tuples of consecutive elements. If n is greater than the length of the list, an empty list is returned.
Acts as a transducer if a transformer is given in list position.
See also transduce.
R.aperture(2, [1, 2, 3, 4, 5]); //=> [[1, 2], [2, 3], [3, 4], [4, 5]] R.aperture(3, [1, 2, 3, 4, 5]); //=> [[1, 2, 3], [2, 3, 4], [3, 4, 5]] R.aperture(7, [1, 2, 3, 4, 5]); //=> []
append Added in v0.1.0
a → [a] → [a]
Parameters
-
elThe element to add to the end of the new list.
-
listThe list of elements to add a new item to. list.
Returns
- Array A new list containing the elements of the old list followed by `el`.
Returns a new list containing the contents of the given list, followed by the given element.
See also prepend.
R.append('tests', ['write', 'more']); //=> ['write', 'more', 'tests']
R.append('tests', []); //=> ['tests']
R.append(['tests'], ['write', 'more']); //=> ['write', 'more', ['tests']] apply Added in v0.7.0
(*… → a) → [*] → a
Parameters
-
fnThe function which will be called with
args -
argsThe arguments to call
fnwith
Returns
- * result The result, equivalent to `fn(...args)`
Applies function fn to the argument list args. This is useful for creating a fixed-arity function from a variadic function. fn should be a bound function if context is significant.
const nums = [1, 2, 3, -99, 42, 6, 7]; R.apply(Math.max, nums); //=> 42
applySpec Added in v0.20.0
{k: ((a, b, …, m) → v)} → ((a, b, …, m) → {k: v}) Parameters
-
specan object recursively mapping properties to functions for producing the values for these properties.
Returns
- function A function that returns an object of the same structure as `spec', with each property set to the value returned by calling its associated function with the supplied arguments.
Given a spec object recursively mapping properties to functions, creates a function producing an object of the same structure, by mapping each property to the result of calling its associated function with the supplied arguments.
const getMetrics = R.applySpec({
sum: R.add,
nested: { mul: R.multiply }
});
getMetrics(2, 4); // => { sum: 6, nested: { mul: 8 } } applyTo Added in v0.25.0
a → (a → b) → b
Parameters
-
xThe value
-
fThe function to apply
Returns
- * The result of applying `f` to `x`
Takes a value and applies a function to it.
This function is also known as the thrush combinator.
const t42 = R.applyTo(42); t42(R.identity); //=> 42 t42(R.add(1)); //=> 43
ascend Added in v0.23.0
Ord b => (a → b) → a → a → Number
Parameters
-
fnA function of arity one that returns a value that can be compared
-
aThe first item to be compared.
-
bThe second item to be compared.
Returns
- Number `-1` if fn(a) < fn(b), `1` if fn(b) < fn(a), otherwise `0`
Makes an ascending comparator function out of a function that returns a value that can be compared with < and >.
See also descend, ascendNatural, descendNatural.
const byAge = R.ascend(R.prop('age'));
const people = [
{ name: 'Emma', age: 70 },
{ name: 'Peter', age: 78 },
{ name: 'Mikhail', age: 62 },
];
const peopleByYoungestFirst = R.sort(byAge, people);
//=> [{ name: 'Mikhail', age: 62 },{ name: 'Emma', age: 70 }, { name: 'Peter', age: 78 }] ascendNatural Added in v0.30.1
s → (a → String) → a → a → Number
Parameters
-
localesA string with a BCP 47 language tag, or an array of such strings. Corresponds to the locales parameter of the Intl.Collator() constructor.
-
fnA function of arity one that returns a string that can be compared
-
aThe first item to be compared.
-
bThe second item to be compared.
Returns
- Number `-1` if a occurs before b, `1` if a occurs after b, otherwise `0`
Makes an ascending comparator function out of a function that returns a value that can be compared with natural sorting using localeCompare.
See also ascend.
const unsorted = ['3', '1', '10', 'Ørjan', 'Bob', 'Älva'];
R.sort(R.ascendNatural('en', R.identity), unsorted);
// => ['1', '3', '10', 'Älva', 'Bob', 'Ørjan']
R.sort(R.ascendNatural('sv', R.identity), unsorted);
// => ['1', '3', '10', 'Bob', 'Älva', 'Ørjan']
R.sort(R.ascend(R.identity), unsorted);
// => ['1', '10', '3', 'Bob', 'Älva', 'Ørjan'] assoc Added in v0.8.0
Idx → a → {k: v} → {k: v} Idx = String | IntParameters
-
propThe property name to set
-
valThe new value
-
objThe object to clone
Returns
- Object A new object equivalent to the original except for the changed property.
Makes a shallow clone of an object, setting or overriding the specified property with the given value. Note that this copies and flattens prototype properties onto the new object as well. All non-primitive properties are copied by reference.
R.assoc('c', 3, {a: 1, b: 2}); //=> {a: 1, b: 2, c: 3}
R.assoc(4, 3, [1, 2]); //=> [1, 2, undefined, undefined, 3]
R.assoc(-1, 3, [1, 2]); //=> [1, 3] assocPath Added in v0.8.0
[Idx] → a → {a} → {a} Idx = String | Int | SymbolParameters
-
paththe path to set
-
valThe new value
-
objThe object to clone
Returns
- Object A new object equivalent to the original except along the specified path.
Makes a shallow clone of an object, setting or overriding the nodes required to create the given path, and placing the specific value at the tail end of that path. Note that this copies and flattens prototype properties onto the new object as well. All non-primitive properties are copied by reference.
See also dissocPath.
R.assocPath(['a', 'b', 'c'], 42, {a: {b: {c: 0}}}); //=> {a: {b: {c: 42}}}
// Any missing or non-object keys in path will be overridden
R.assocPath(['a', 'b', 'c'], 42, {a: 5}); //=> {a: {b: {c: 42}}}
R.assocPath(['a', 1, 'c'], 42, {a: []}); // => {a: [undefined, {c: 42}]}
R.assocPath(['a', -1], 42, {a: [1, 2]}); // => {a: [1, 42]} binary Added in v0.2.0
(a → b → c → … → z) → ((a, b) → z)
Parameters
-
fnThe function to wrap.
Returns
- function A new function wrapping `fn`. The new function is guaranteed to be of arity 2.
Wraps a function of any arity (including nullary) in a function that accepts exactly 2 parameters. Any extraneous parameters will not be passed to the supplied function.
const takesThreeArgs = function(a, b, c) {
return [a, b, c];
};
takesThreeArgs.length; //=> 3
takesThreeArgs(1, 2, 3); //=> [1, 2, 3]
const takesTwoArgs = R.binary(takesThreeArgs);
takesTwoArgs.length; //=> 2
// Only 2 arguments are passed to the wrapped function
takesTwoArgs(1, 2, 3); //=> [1, 2, undefined] bind Added in v0.6.0
(* → *) → {*} → (* → *) Parameters
-
fnThe function to bind to context
-
thisObjThe context to bind
fnto
Returns
- function A function that will execute in the context of `thisObj`.
Creates a function that is bound to a context. Note: R.bind does not provide the additional argument-binding capabilities of Function.prototype.bind.
See also partial.
const log = R.bind(console.log, console);
R.pipe(R.assoc('a', 2), R.tap(log), R.assoc('a', 3))({a: 1}); //=> {a: 3}
// logs {a: 2} both Added in v0.12.0
(*… → Boolean) → (*… → Boolean) → (*… → Boolean)
Parameters
-
fA predicate
-
gAnother predicate
Returns
- function a function that applies its arguments to `f` and `g` and `&&`s their outputs together.
A function which calls the two provided functions and returns the && of the results. It returns the result of the first function if it is false-y and the result of the second function otherwise. Note that this is short-circuited, meaning that the second function will not be invoked if the first returns a false-y value.
In addition to functions, R.both also accepts any fantasy-land compatible applicative functor.
See also either, allPass, and.
const gt10 = R.gt(R.__, 10) const lt20 = R.lt(R.__, 20) const f = R.both(gt10, lt20); f(15); //=> true f(30); //=> false R.both(Maybe.Just(false), Maybe.Just(55)); // => Maybe.Just(false) R.both([false, false, 'a'], [11]); //=> [false, false, 11]
call Added in v0.9.0
((*… → a), *…) → a
Parameters
-
fnThe function to apply to the remaining arguments.
-
argsAny number of positional arguments.
Returns
- *
Returns the result of calling its first argument with the remaining arguments. This is occasionally useful as a converging function for R.converge: the first branch can produce a function while the remaining branches produce values to be passed to that function as its arguments.
See also apply.
R.call(R.add, 1, 2); //=> 3
const indentN = R.pipe(
R.repeat(' '),
R.join(''),
R.replace(/^(?!$)/gm)
);
const format = R.converge(
R.call,
[
R.pipe(R.prop('indent'), indentN),
R.prop('value')
]
);
format({indent: 2, value: 'foo\nbar\nbaz\n'}); //=> ' foo\n bar\n baz\n' chain Added in v0.3.0
Chain m => (a → m b) → m a → m b
Parameters
-
fnThe function to map with
-
listThe list to map over
Returns
- Array The result of flat-mapping `list` with `fn`
chain maps a function over a list and concatenates the results. chain is also known as flatMap in some libraries.
Dispatches to the chain method of the second argument, if present, according to the FantasyLand Chain spec.
If second argument is a function, chain(f, g)(x) is equivalent to f(g(x), x).
Acts as a transducer if a transformer is given in list position.
const duplicate = n => [n, n]; R.chain(duplicate, [1, 2, 3]); //=> [1, 1, 2, 2, 3, 3] R.chain(R.append, R.head)([1, 2, 3]); //=> [1, 2, 3, 1]
clamp Added in v0.20.0
Ord a => a → a → a → a
Parameters
-
minimumThe lower limit of the clamp (inclusive)
-
maximumThe upper limit of the clamp (inclusive)
-
valueValue to be clamped
Returns
- Number Returns `minimum` when `val < minimum`, `maximum` when `val > maximum`, returns `val` otherwise
Restricts a number to be within a range.
Also works for other ordered types such as Strings and Dates.
R.clamp(1, 10, -5) // => 1 R.clamp(1, 10, 15) // => 10 R.clamp(1, 10, 4) // => 4
clone Added in v0.1.0
{*} → {*} Parameters
-
valueThe object or array to clone
Returns
- * A deeply cloned copy of `val`
Creates a deep copy of the source that can be used in place of the source object without retaining any references to it. The source object may contain (nested) Arrays and Objects, Numbers, Strings, Booleans and Dates. Functions are assigned by reference rather than copied.
Dispatches to a clone method if present.
Note that if the source object has multiple nodes that share a reference, the returned object will have the same structure, but the references will be pointed to the location within the cloned value.
const objects = [{}, {}, {}];
const objectsClone = R.clone(objects);
objects === objectsClone; //=> false
objects[0] === objectsClone[0]; //=> false collectBy Added in v0.28.0
Idx a => (b → a) → [b] → [[b]]
Idx = String | Int | SymbolParameters
-
fnFunction :: a -> Idx
-
listThe array to group
Returns
- Array An array of arrays where each sub-array contains items for which the String-returning function has returned the same value.
Splits a list into sub-lists, based on the result of calling a key-returning function on each element, and grouping the results according to values returned.
R.collectBy(R.prop('type'), [
{type: 'breakfast', item: '☕️'},
{type: 'lunch', item: '🌯'},
{type: 'dinner', item: '🍝'},
{type: 'breakfast', item: '🥐'},
{type: 'lunch', item: '🍕'}
]);
// [ [ {type: 'breakfast', item: '☕️'},
// {type: 'breakfast', item: '🥐'} ],
// [ {type: 'lunch', item: '🌯'},
// {type: 'lunch', item: '🍕'} ],
// [ {type: 'dinner', item: '🍝'} ] ] comparator Added in v0.1.0
((a, b) → Boolean) → ((a, b) → Number)
Parameters
-
predA predicate function of arity two which will return
trueif the first argument is less than the second,falseotherwise
Returns
- function A Function :: a -> b -> Int that returns `-1` if a < b, `1` if b < a, otherwise `0`
Makes a comparator function out of a function that reports whether the first element is less than the second.
const byAge = R.comparator((a, b) => a.age < b.age);
const people = [
{ name: 'Emma', age: 70 },
{ name: 'Peter', age: 78 },
{ name: 'Mikhail', age: 62 },
];
const peopleByIncreasingAge = R.sort(byAge, people);
//=> [{ name: 'Mikhail', age: 62 },{ name: 'Emma', age: 70 }, { name: 'Peter', age: 78 }] complement Added in v0.12.0
(*… → *) → (*… → Boolean)
Parameters
-
f
Returns
- function
Takes a function f and returns a function g such that if called with the same arguments when f returns a "truthy" value, g returns false and when f returns a "falsy" value g returns true.
R.complement may be applied to any functor
See also not.
const isNotNil = R.complement(R.isNil); R.isNil(null); //=> true isNotNil(null); //=> false R.isNil(7); //=> false isNotNil(7); //=> true
compose Added in v0.1.0
((y → z), (x → y), …, (o → p), ((a, b, …, n) → o)) → ((a, b, …, n) → z)
Parameters
-
...functionsThe functions to compose
Returns
- function
Performs right-to-left function composition. The last argument may have any arity; the remaining arguments must be unary.
Note: The result of compose is not automatically curried.
See also pipe.
const classyGreeting = (firstName, lastName) => "The name's " + lastName + ", " + firstName + " " + lastName
const yellGreeting = R.compose(R.toUpper, classyGreeting);
yellGreeting('James', 'Bond'); //=> "THE NAME'S BOND, JAMES BOND"
R.compose(Math.abs, R.add(1), R.multiply(2))(-4) //=> 7 composeWith Added in v0.26.0
((* → *), [(y → z), (x → y), …, (o → p), ((a, b, …, n) → o)]) → ((a, b, …, n) → z)
Parameters
-
transformerThe transforming function
-
functionsThe functions to compose
Returns
- function
Performs right-to-left function composition using transforming function. The last function may have any arity; the remaining functions must be unary. Unlike compose, functions are passed in an array.
Note: The result of composeWith is not automatically curried. Transforming function is not used on the last argument.
const composeWhileNotNil = R.composeWith((f, res) => R.isNil(res) ? res : f(res));
composeWhileNotNil([R.inc, R.prop('age')])({age: 1}) //=> 2
composeWhileNotNil([R.inc, R.prop('age')])({}) //=> undefined concat Added in v0.1.0
[a] → [a] → [a]
String → String → StringParameters
-
firstListThe first list
-
secondListThe second list
Returns
- Array A list consisting of the elements of `firstList` followed by the elements of `secondList`.
Returns the result of concatenating the given lists or strings.
Note: R.concat expects both arguments to be of the same type, unlike the native Array.prototype.concat method. It will throw an error if you concat an Array with a non-Array value.
Dispatches to the concat method of the first argument, if present. Can also concatenate two members of a fantasy-land compatible semigroup.
R.concat('ABC', 'DEF'); // 'ABCDEF'
R.concat([4, 5, 6], [1, 2, 3]); //=> [4, 5, 6, 1, 2, 3]
R.concat([], []); //=> [] cond Added in v0.6.0
[[(*… → Boolean),(*… → *)]] → (*… → *)
Parameters
-
pairsA list of [predicate, transformer]
Returns
- function
Returns a function, fn, which encapsulates if/else, if/else, ... logic. R.cond takes a list of [predicate, transformer] pairs. All of the arguments to fn are applied to each of the predicates in turn until one returns a "truthy" value, at which point fn returns the result of applying its arguments to the corresponding transformer. If none of the predicates matches, fn returns undefined.
Please note: This is not a direct substitute for a switch statement. Remember that both elements of every pair passed to cond are functions, and cond returns a function.
See also ifElse, unless, when.
const fn = R.cond([
[R.equals(0), R.always('water freezes at 0°C')],
[R.equals(100), R.always('water boils at 100°C')],
[R.T, temp => 'nothing special happens at ' + temp + '°C']
]);
fn(0); //=> 'water freezes at 0°C'
fn(50); //=> 'nothing special happens at 50°C'
fn(100); //=> 'water boils at 100°C' construct Added in v0.1.0
(* → {*}) → (* → {*}) Parameters
-
fnThe constructor function to wrap.
Returns
- function A wrapped, curried constructor function.
Wraps a constructor function inside a curried function that can be called with the same arguments and returns the same type.
See also invoker.
// Constructor function
function Animal(kind) {
this.kind = kind;
};
Animal.prototype.sighting = function() {
return "It's a " + this.kind + "!";
}
const AnimalConstructor = R.construct(Animal)
// Notice we no longer need the 'new' keyword:
AnimalConstructor('Pig'); //=> {"kind": "Pig", "sighting": function (){...}};
const animalTypes = ["Lion", "Tiger", "Bear"];
const animalSighting = R.invoker(0, 'sighting');
const sightNewAnimal = R.compose(animalSighting, AnimalConstructor);
R.map(sightNewAnimal, animalTypes); //=> ["It's a Lion!", "It's a Tiger!", "It's a Bear!"] constructN Added in v0.4.0
Number → (* → {*}) → (* → {*}) Parameters
-
nThe arity of the constructor function.
-
FnThe constructor function to wrap.
Returns
- function A wrapped, curried constructor function.
Wraps a constructor function inside a curried function that can be called with the same arguments and returns the same type. The arity of the function returned is specified to allow using variadic constructor functions.
// Variadic Constructor function
function Salad() {
this.ingredients = arguments;
}
Salad.prototype.recipe = function() {
const instructions = R.map(ingredient => 'Add a dollop of ' + ingredient, this.ingredients);
return R.join('\n', instructions);
};
const ThreeLayerSalad = R.constructN(3, Salad);
// Notice we no longer need the 'new' keyword, and the constructor is curried for 3 arguments.
const salad = ThreeLayerSalad('Mayonnaise')('Potato Chips')('Ketchup');
console.log(salad.recipe());
// Add a dollop of Mayonnaise
// Add a dollop of Potato Chips
// Add a dollop of Ketchup converge Added in v0.4.2
((x1, x2, …) → z) → [((a, b, …) → x1), ((a, b, …) → x2), …] → (a → b → … → z)
Parameters
-
afterA function.
afterwill be invoked with the return values offn1andfn2as its arguments. -
functionsA list of functions.
Returns
- function A new function.
Accepts a converging function and a list of branching functions and returns a new function. The arity of the new function is the same as the arity of the longest branching function. When invoked, this new function is applied to some arguments, and each branching function is applied to those same arguments. The results of each branching function are passed as arguments to the converging function to produce the return value.
See also useWith.
const average = R.converge(R.divide, [R.sum, R.length])
average([1, 2, 3, 4, 5, 6, 7]) //=> 4
const strangeConcat = R.converge(R.concat, [R.toUpper, R.toLower])
strangeConcat("Yodel") //=> "YODELyodel" count Added in v0.28.0
(a → Boolean) → [a] → Number
Parameters
-
predicateThe function to match items against.
-
listThe list to count elements from.
Returns
- Number The count of items matching the predicate.
Returns the number of items in a given list matching the predicate f
const even = x => x % 2 == 0; R.count(even, [1, 2, 3, 4, 5]); // => 2 R.map(R.count(even), [[1, 1, 1], [2, 3, 4, 5], [6]]); // => [0, 2, 1]
countBy Added in v0.1.0
(a → String) → [a] → {*} Parameters
-
fnThe function used to map values to keys.
-
listThe list to count elements from.
Returns
- Object An object mapping keys to number of occurrences in the list.
Counts the elements of a list according to how many match each value of a key generated by the supplied function. Returns an object mapping the keys produced by fn to the number of occurrences in the list. Note that all keys are coerced to strings because of how JavaScript objects work.
Acts as a transducer if a transformer is given in list position.
const numbers = [1.0, 1.1, 1.2, 2.0, 3.0, 2.2];
R.countBy(Math.floor)(numbers); //=> {'1': 3, '2': 2, '3': 1}
const letters = ['a', 'b', 'A', 'a', 'B', 'c'];
R.countBy(R.toLower)(letters); //=> {'a': 3, 'b': 2, 'c': 1} curry Added in v0.1.0
(* → a) → (* → a)
Parameters
-
fnThe function to curry.
Returns
- function A new, curried function.
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 R.curry(f), the following are equivalent:
g(1)(2)(3)g(1)(2, 3)g(1, 2)(3)g(1, 2, 3)
Secondly, the special placeholder value R.__ may be used to specify "gaps", allowing partial application of any combination of arguments, regardless of their positions. If g is as above and _ is R.__, the following are equivalent:
g(1, 2, 3)g(_, 2, 3)(1)g(_, _, 3)(1)(2)g(_, _, 3)(1, 2)g(_, 2)(1)(3)g(_, 2)(1, 3)g(_, 2)(_, 3)(1)
Please note that default parameters don't count towards a function arity and therefore curry won't work well with those.
const addFourNumbers = (a, b, c, d) => a + b + c + d; const curriedAddFourNumbers = R.curry(addFourNumbers); const f = curriedAddFourNumbers(1, 2); const g = f(3); g(4); //=> 10 // R.curry not working well with default parameters const h = R.curry((a, b, c = 2) => a + b + c); h(1)(2)(7); //=> Error! (`3` is not a function!)
curryN Added in v0.5.0
Number → (* → a) → (* → a)
Parameters
-
lengthThe arity for the returned function.
-
fnThe function to curry.
Returns
- function A new, curried function.
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 R.curryN(3, f), the following are equivalent:
g(1)(2)(3)g(1)(2, 3)g(1, 2)(3)g(1, 2, 3)
Secondly, the special placeholder value R.__ may be used to specify "gaps", allowing partial application of any combination of arguments, regardless of their positions. If g is as above and _ is R.__, the following are equivalent:
g(1, 2, 3)g(_, 2, 3)(1)g(_, _, 3)(1)(2)g(_, _, 3)(1, 2)g(_, 2)(1)(3)g(_, 2)(1, 3)g(_, 2)(_, 3)(1)
See also curry.
const sumArgs = (...args) => R.sum(args); const curriedAddFourNumbers = R.curryN(4, sumArgs); const f = curriedAddFourNumbers(1, 2); const g = f(3); g(4); //=> 10
dec Added in v0.9.0
Number → Number
Parameters
-
n
Returns
- Number n - 1
Decrements its argument.
See also inc.
R.dec(42); //=> 41
defaultTo Added in v0.10.0
a → b → a | b
Parameters
-
defaultThe default value.
-
valvalwill be returned instead ofdefaultunlessvalisnull,undefinedorNaN.
Returns
- * The second value if it is not `null`, `undefined` or `NaN`, otherwise the default value
Returns the second argument if it is not null, undefined or NaN; otherwise the first argument is returned.
const defaultTo42 = R.defaultTo(42);
defaultTo42(null); //=> 42
defaultTo42(undefined); //=> 42
defaultTo42(false); //=> false
defaultTo42('Ramda'); //=> 'Ramda'
// parseInt('string') results in NaN
defaultTo42(parseInt('string')); //=> 42 descend Added in v0.23.0
Ord b => (a → b) → a → a → Number
Parameters
-
fnA function of arity one that returns a value that can be compared
-
aThe first item to be compared.
-
bThe second item to be compared.
Returns
- Number `-1` if fn(a) > fn(b), `1` if fn(b) > fn(a), otherwise `0`
Makes a descending comparator function out of a function that returns a value that can be compared with < and >.
See also ascend, descendNatural, ascendNatural.
const byAge = R.descend(R.prop('age'));
const people = [
{ name: 'Emma', age: 70 },
{ name: 'Peter', age: 78 },
{ name: 'Mikhail', age: 62 },
];
const peopleByOldestFirst = R.sort(byAge, people);
//=> [{ name: 'Peter', age: 78 }, { name: 'Emma', age: 70 }, { name: 'Mikhail', age: 62 }] descendNatural Added in v0.30.1
s → (a → String) → a → a → Number
Parameters
-
localesA string with a BCP 47 language tag, or an array of such strings. Corresponds to the locales parameter of the Intl.Collator() constructor.
-
fnA function of arity one that returns a string that can be compared
-
aThe first item to be compared.
-
bThe second item to be compared.
Returns
- Number `-1` if a occurs after b, `1` if a occurs before b, otherwise `0`
Makes a descending comparator function out of a function that returns a value that can be compared with natural sorting using localeCompare.
See also descend.
const unsorted = ['3', '1', '10', 'Ørjan', 'Bob', 'Älva'];
R.sort(R.descendNatural('en', R.identity), unsorted);
// => ['Ørjan', 'Bob', 'Älva', '10', '3', '1']
R.sort(R.descendNatural('sv', R.identity), unsorted);
// => ['Ørjan', 'Älva', 'Bob', '10', '3', '1']
R.sort(R.descend(R.identity), unsorted);
// => ['Ørjan', 'Älva', 'Bob', '3', '10', '1'] difference Added in v0.1.0
[*] → [*] → [*]
Parameters
-
list1The first list.
-
list2The second list.
Returns
- Array The elements in `list1` that are not in `list2`.
Finds the set (i.e. no duplicates) of all elements in the first list not contained in the second list. Objects and Arrays are compared in terms of value equality, not reference equality.
See also differenceWith, symmetricDifference, symmetricDifferenceWith, without.
R.difference([1,2,3,4], [7,6,5,4,3]); //=> [1,2]
R.difference([7,6,5,4,3], [1,2,3,4]); //=> [7,6,5]
R.difference([{a: 1}, {b: 2}], [{a: 1}, {c: 3}]) //=> [{b: 2}] differenceWith Added in v0.1.0
((a, a) → Boolean) → [a] → [a] → [a]
Parameters
-
predA predicate used to test whether two items are equal.
-
list1The first list.
-
list2The second list.
Returns
- Array The elements in `list1` that are not in `list2`.
Finds the set (i.e. no duplicates) of all elements in the first list not contained in the second list. Duplication is determined according to the value returned by applying the supplied predicate to two list elements.
See also difference, symmetricDifference, symmetricDifferenceWith.
const cmp = (x, y) => x.a === y.a;
const l1 = [{a: 1}, {a: 2}, {a: 3}];
const l2 = [{a: 3}, {a: 4}];
R.differenceWith(cmp, l1, l2); //=> [{a: 1}, {a: 2}]
R.differenceWith(R.equals, [1, 2, 3, 3, 3], []); //=> [1, 2, 3]
R.differenceWith(R.equals, [1, 2, 3, 3, 3], [1]); //=> [2, 3] dissoc Added in v0.10.0
String → {k: v} → {k: v} Parameters
-
propThe name of the property to dissociate
-
objThe object to clone
Returns
- Object A new object equivalent to the original but without the specified property
Returns a new object that does not contain a prop property.
R.dissoc('b', {a: 1, b: 2, c: 3}); //=> {a: 1, c: 3} dissocPath Added in v0.11.0
[Idx] → {k: v} → {k: v} Idx = String | Int | SymbolParameters
-
pathThe path to the value to omit
-
objThe object to clone
Returns
- Object A new object without the property at path
Makes a shallow clone of an object, omitting the property at the given path. Note that this copies and flattens prototype properties onto the new object as well. All non-primitive properties are copied by reference.
See also assocPath.
R.dissocPath(['a', 'b', 'c'], {a: {b: {c: 42}}}); //=> {a: {b: {}}} divide Added in v0.1.0
Number → Number → Number
Parameters
-
aThe first value.
-
bThe second value.
Returns
- Number The result of `a / b`.
Divides two numbers. Equivalent to a / b.
See also multiply.
R.divide(71, 100); //=> 0.71 const half = R.divide(R.__, 2); half(42); //=> 21 const reciprocal = R.divide(1); reciprocal(4); //=> 0.25
drop Added in v0.1.0
Number → [a] → [a]
Number → String → StringParameters
-
n -
list
Returns
- * A copy of list without the first `n` elements
Returns all but the first n elements of the given list, string, or transducer/transformer (or object with a drop method).
Dispatches to the drop method of the second argument, if present.
See also take, transduce, dropLast, dropWhile.
R.drop(1, ['foo', 'bar', 'baz']); //=> ['bar', 'baz'] R.drop(2, ['foo', 'bar', 'baz']); //=> ['baz'] R.drop(3, ['foo', 'bar', 'baz']); //=> [] R.drop(4, ['foo', 'bar', 'baz']); //=> [] R.drop(3, 'ramda'); //=> 'da'
dropLast Added in v0.16.0
Number → [a] → [a]
Number → String → StringParameters
-
nThe number of elements of
listto skip. -
listThe list of elements to consider.
Returns
- Array A copy of the list with only the first `list.length - n` elements
Returns a list containing all but the last n elements of the given list.
Acts as a transducer if a transformer is given in list position.
See also takeLast, drop, dropWhile, dropLastWhile.
R.dropLast(1, ['foo', 'bar', 'baz']); //=> ['foo', 'bar'] R.dropLast(2, ['foo', 'bar', 'baz']); //=> ['foo'] R.dropLast(3, ['foo', 'bar', 'baz']); //=> [] R.dropLast(4, ['foo', 'bar', 'baz']); //=> [] R.dropLast(3, 'ramda'); //=> 'ra'
dropLastWhile Added in v0.16.0
(a → Boolean) → [a] → [a]
(a → Boolean) → String → StringParameters
-
predicateThe function to be called on each element
-
xsThe collection to iterate over.
Returns
- Array A new array without any trailing elements that return `falsy` values from the `predicate`.
Returns a new list excluding all the tailing elements of a given list which satisfy the supplied predicate function. It passes each value from the right to the supplied predicate function, skipping elements until the predicate function returns a falsy value. The predicate function is applied to one argument: (value).
Acts as a transducer if a transformer is given in list position.
See also takeLastWhile, addIndex, drop, dropWhile.
const lteThree = x => x <= 3; R.dropLastWhile(lteThree, [1, 2, 3, 4, 3, 2, 1]); //=> [1, 2, 3, 4] R.dropLastWhile(x => x !== 'd' , 'Ramda'); //=> 'Ramd'
dropRepeats Added in v0.14.0
[a] → [a]
Parameters
-
listThe array to consider.
Returns
- Array `list` without repeating elements.
Returns a new list without any consecutively repeating elements. R.equals is used to determine equality.
Acts as a transducer if a transformer is given in list position.
See also transduce.
R.dropRepeats([1, 1, 1, 2, 3, 4, 4, 2, 2]); //=> [1, 2, 3, 4, 2]
dropRepeatsBy Added in v0.29.0
(a → b) → [a] → [a]
Parameters
-
fnA function used to produce a value to use during comparisons.
-
listThe array to consider.
Returns
- Array `list` without repeating elements.
Returns a new list without any consecutively repeating elements, based upon the value returned by applying the supplied function to each list element. R.equals is used to determine equality.
Acts as a transducer if a transformer is given in list position.
See also transduce.
R.dropRepeatsBy(Math.abs, [1, -1, -1, 2, 3, -4, 4, 2, 2]); //=> [1, 2, 3, -4, 2]
dropRepeatsWith Added in v0.14.0
((a, a) → Boolean) → [a] → [a]
Parameters
-
predA predicate used to test whether two items are equal.
-
listThe array to consider.
Returns
- Array `list` without repeating elements.
Returns a new list without any consecutively repeating elements. Equality is determined by applying the supplied predicate to each pair of consecutive elements. The first element in a series of equal elements will be preserved.
Acts as a transducer if a transformer is given in list position.
See also transduce.
const l = [1, -1, 1, 3, 4, -4, -4, -5, 5, 3, 3]; R.dropRepeatsWith(R.eqBy(Math.abs), l); //=> [1, 3, 4, -5, 3]
dropWhile Added in v0.9.0
(a → Boolean) → [a] → [a]
(a → Boolean) → String → StringParameters
-
fnThe function called per iteration.
-
xsThe collection to iterate over.
Returns
- Array A new array.
Returns a new list excluding the leading elements of a given list which satisfy the supplied predicate function. It passes each value to the supplied predicate function, skipping elements while the predicate function returns true. The predicate function is applied to one argument: (value).
Dispatches to the dropWhile method of the second argument, if present.
Acts as a transducer if a transformer is given in list position.
See also takeWhile, transduce, addIndex.
const lteTwo = x => x <= 2; R.dropWhile(lteTwo, [1, 2, 3, 4, 3, 2, 1]); //=> [3, 4, 3, 2, 1] R.dropWhile(x => x !== 'd' , 'Ramda'); //=> 'da'
either Added in v0.12.0
(*… → Boolean) → (*… → Boolean) → (*… → Boolean)
Parameters
-
fa predicate
-
ganother predicate
Returns
- function a function that applies its arguments to `f` and `g` and `||`s their outputs together.
A function wrapping calls to the two functions in an || operation, returning the result of the first function if it is truth-y and the result of the second function otherwise. Note that this is short-circuited, meaning that the second function will not be invoked if the first returns a truth-y value.
In addition to functions, R.either also accepts any fantasy-land compatible applicative functor.
const gt10 = x => x > 10; const even = x => x % 2 === 0; const f = R.either(gt10, even); f(101); //=> true f(8); //=> true R.either(Maybe.Just(false), Maybe.Just(55)); // => Maybe.Just(55) R.either([false, false, 'a'], [11]) // => [11, 11, "a"]
empty Added in v0.3.0
a → a
Parameters
-
x
Returns
- *
Returns the empty value of its argument's type. Ramda defines the empty value of Array ([]), Object ({}), String (''), Map (new Map()), Set (new Set()), TypedArray (Uint8Array [], Float32Array [], etc), and Arguments. Other types are supported if they define <Type>.empty, <Type>.prototype.empty or implement the FantasyLand Monoid spec.
Dispatches to the empty method of the first argument, if present.
R.empty(Just(42)); //=> Nothing()
R.empty([1, 2, 3]); //=> []
R.empty('unicorns'); //=> ''
R.empty({x: 1, y: 2}); //=> {}
R.empty(Uint8Array.from('123')); //=> Uint8Array []
R.empty(Set); //=> Set() endsWith Added in v0.24.0
[a] → [a] → Boolean
String → String → BooleanParameters
-
suffix -
list
Returns
- Boolean
Checks if a list ends with the provided sublist.
Similarly, checks if a string ends with the provided substring.
See also startsWith.
R.endsWith('c', 'abc') //=> true
R.endsWith('b', 'abc') //=> false
R.endsWith(['c'], ['a', 'b', 'c']) //=> true
R.endsWith(['b'], ['a', 'b', 'c']) //=> false eqBy Added in v0.18.0
(a → b) → a → a → Boolean
Parameters
-
f -
x -
y
Returns
- Boolean
Takes a function and two values in its domain and returns true if the values map to the same value in the codomain; false otherwise.
R.eqBy(Math.abs, 5, -5); //=> true
eqProps Added in v0.1.0
k → {k: v} → {k: v} → Boolean Parameters
-
propThe name of the property to compare
-
obj1 -
obj2
Returns
- Boolean
Reports whether two objects have the same value, in R.equals terms, for the specified property. Useful as a curried predicate.
const o1 = { a: 1, b: 2, c: 3, d: 4 };
const o2 = { a: 10, b: 20, c: 3, d: 40 };
R.eqProps('a', o1, o2); //=> false
R.eqProps('c', o1, o2); //=> true equals Added in v0.15.0
a → b → Boolean
Parameters
-
a -
b
Returns
- Boolean
Returns true if its arguments are equivalent, false otherwise. Handles cyclical data structures.
Dispatches symmetrically to the equals methods of both arguments, if present.
R.equals(1, 1); //=> true
R.equals(1, '1'); //=> false
R.equals([1, 2, 3], [1, 2, 3]); //=> true
const a = {}; a.v = a;
const b = {}; b.v = b;
R.equals(a, b); //=> true evolve Added in v0.9.0
{k: (v → v)} → {k: v} → {k: v} Parameters
-
transformationsThe object specifying transformation functions to apply to the object.
-
objectThe object to be transformed.
Returns
- Object The transformed object.
Creates a new object by recursively evolving a shallow copy of object, according to the transformation functions. All non-primitive properties are copied by reference.
A transformation function will not be invoked if its corresponding key does not exist in the evolved object.
const tomato = {firstName: ' Tomato ', data: {elapsed: 100, remaining: 1400}, id:123};
const transformations = {
firstName: R.trim,
lastName: R.trim, // Will not get invoked.
data: {elapsed: R.add(1), remaining: R.add(-1)}
};
R.evolve(transformations, tomato); //=> {firstName: 'Tomato', data: {elapsed: 101, remaining: 1399}, id:123} F Added in v0.9.0
* → Boolean
Parameters
Returns
- Boolean
A function that always returns false. Any passed in parameters are ignored.
See also T.
R.F(); //=> false
filter Added in v0.1.0
Filterable f => (a → Boolean) → f a → f a
Parameters
-
pred -
filterable
Returns
- Array Filterable
Takes a predicate and a Filterable, and returns a new filterable of the same type containing the members of the given filterable which satisfy the given predicate. Filterable objects include plain objects, Maps, or any object that has a filter method such as Array.
Dispatches to the filter method of the second argument, if present.
Acts as a transducer if a transformer is given in list position.
See also reject, transduce, addIndex.
const isEven = n => n % 2 === 0;
R.filter(isEven, [1, 2, 3, 4]); //=> [2, 4]
R.filter(isEven, {a: 1, b: 2, c: 3, d: 4}); //=> {b: 2, d: 4} find Added in v0.1.0
(a → Boolean) → [a] → a | undefined
Parameters
-
fnThe predicate function used to determine if the element is the desired one.
-
listThe array to consider.
Returns
- Object The element found, or `undefined`.
Returns the first element of the list which matches the predicate, or undefined if no element matches.
Dispatches to the find method of the second argument, if present.
Acts as a transducer if a transformer is given in list position.
See also transduce.
const xs = [{a: 1}, {a: 2}, {a: 3}];
R.find(R.propEq(2, 'a'))(xs); //=> {a: 2}
R.find(R.propEq(4, 'a'))(xs); //=> undefined findIndex Added in v0.1.1
(a → Boolean) → [a] → Number
Parameters
-
fnThe predicate function used to determine if the element is the desired one.
-
listThe array to consider.
Returns
- Number The index of the element found, or `-1`.
Returns the index of the first element of the list which matches the predicate, or -1 if no element matches.
Acts as a transducer if a transformer is given in list position.
const xs = [{a: 1}, {a: 2}, {a: 3}];
R.findIndex(R.propEq(2, 'a'))(xs); //=> 1
R.findIndex(R.propEq(4, 'a'))(xs); //=> -1 findLast Added in v0.1.1
(a → Boolean) → [a] → a | undefined
Parameters
-
fnThe predicate function used to determine if the element is the desired one.
-
listThe array to consider.
Returns
- Object The element found, or `undefined`.
Returns the last element of the list which matches the predicate, or undefined if no element matches.
Acts as a transducer if a transformer is given in list position.
See also transduce.
const xs = [{a: 1, b: 0}, {a:1, b: 1}];
R.findLast(R.propEq(1, 'a'))(xs); //=> {a: 1, b: 1}
R.findLast(R.propEq(4, 'a'))(xs); //=> undefined findLastIndex Added in v0.1.1
(a → Boolean) → [a] → Number
Parameters
-
fnThe predicate function used to determine if the element is the desired one.
-
listThe array to consider.
Returns
- Number The index of the element found, or `-1`.
Returns the index of the last element of the list which matches the predicate, or -1 if no element matches.
Acts as a transducer if a transformer is given in list position.
See also transduce, lastIndexOf.
const xs = [{a: 1, b: 0}, {a:1, b: 1}];
R.findLastIndex(R.propEq(1, 'a'))(xs); //=> 1
R.findLastIndex(R.propEq(4, 'a'))(xs); //=> -1 flatten Added in v0.1.0
[a] → [b]
Parameters
-
listThe array to consider.
Returns
- Array The flattened list.
Returns a new list by pulling every item out of it (and all its sub-arrays) and putting them in a new array, depth-first.
See also unnest.
R.flatten([1, 2, [3, 4], 5, [6, [7, 8, [9, [10, 11], 12]]]]); //=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
flip Added in v0.1.0
((a, b, c, …) → z) → (b → a → c → … → z)
Parameters
-
fnThe function to invoke with its first two parameters reversed.
Returns
- * The result of invoking `fn` with its first two parameters' order reversed.
Returns a new function much like the supplied one, except that the first two arguments' order is reversed.
const mergeThree = (a, b, c) => [].concat(a, b, c); mergeThree(1, 2, 3); //=> [1, 2, 3] R.flip(mergeThree)(1, 2, 3); //=> [2, 1, 3]
flow Added in v0.30.0
a → [(a → b), …, (y → z)] → z
Parameters
-
aThe seed value
-
pipelinefunctions composing the pipeline
Returns
- * z The result of applying the seed value to the function pipeline
Takes the value of an expression and applies it to a function which is the left-to-right serial composition of the functions given in the second argument.
The functions in the pipeline should be unary functions.
flow is helps to avoid introducing an extra function with named arguments for computing the result of a function pipeline which depends on given initial values. Rather than defining a referential transparent function f = (_x, _y) => R.pipe(g(_x), h(_y), …) which is only later needed once z = f(x, y), the introduction of f, _x and _y can be avoided: z = flow(x, [g, h(y),…]
In some libraries this function is named pipe.
See also pipe.
R.flow(9, [Math.sqrt, R.negate, R.inc]); //=> -2
const personObj = { first: 'Jane', last: 'Doe' };
const fullName = R.flow(personObj, [R.values, R.join(' ')]); //=> "Jane Doe"
const givenName = R.flow(' ', [R.trim, R.when(R.isEmpty, R.always(fullName))]); //=> "Jane Doe" forEach Added in v0.1.1
(a → *) → [a] → [a]
Parameters
-
fnThe function to invoke. Receives one argument,
value. -
listThe list to iterate over.
Returns
- Array The original list.
Iterate over an input list, calling a provided function fn for each element in the list.
fn receives one argument: (value).
Note: R.forEach does not skip deleted or unassigned indices (sparse arrays), unlike the native Array.prototype.forEach method. For more details on this behavior, see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#Description
Also note that, unlike Array.prototype.forEach, Ramda's forEach returns the original array. In some libraries this function is named each.
Dispatches to the forEach method of the second argument, if present.
See also addIndex.
const printXPlusFive = x => console.log(x + 5); R.forEach(printXPlusFive, [1, 2, 3]); //=> [1, 2, 3] // logs 6 // logs 7 // logs 8
forEachObjIndexed Added in v0.23.0
((a, String, StrMap a) → Any) → StrMap a → StrMap a
Parameters
-
fnThe function to invoke. Receives three argument,
value,key,obj. -
objThe object to iterate over.
Returns
- Object The original object.
Iterate over an input object, calling a provided function fn for each key and value in the object.
fn receives three argument: (value, key, obj).
const printKeyConcatValue = (value, key) => console.log(key + ':' + value);
R.forEachObjIndexed(printKeyConcatValue, {x: 1, y: 2}); //=> {x: 1, y: 2}
// logs x:1
// logs y:2 fromPairs Added in v0.3.0
[[k,v]] → {k: v} Parameters
-
pairsAn array of two-element arrays that will be the keys and values of the output object.
Returns
- Object The object made by pairing up `keys` and `values`.
Creates a new object from a list key-value pairs. If a key appears in multiple pairs, the rightmost pair is included in the object.
R.fromPairs([['a', 1], ['b', 2], ['c', 3]]); //=> {a: 1, b: 2, c: 3} groupBy Added in v0.1.0
Idx a => (b → a) → [b] → {a: [b]} Idx = String | Int | SymbolParameters
-
fnFunction :: a -> Idx
-
listThe array to group
Returns
- Object An object with the output of `fn` for keys, mapped to arrays of elements that produced that key when passed to `fn`.
Splits a list into sub-lists stored in an object, based on the result of calling a key-returning function on each element, and grouping the results according to values returned.
Dispatches to the groupBy method of the second argument, if present.
Acts as a transducer if a transformer is given in list position.
See also reduceBy, transduce, indexBy, collectBy.
const byGrade = R.groupBy(function(student) {
const score = student.score;
return score < 65 ? 'F' :
score < 70 ? 'D' :
score < 80 ? 'C' :
score < 90 ? 'B' : 'A';
});
const students = [{name: 'Abby', score: 84},
{name: 'Eddy', score: 58},
// ...
{name: 'Jack', score: 69}];
byGrade(students);
// {
// 'A': [{name: 'Dianne', score: 99}],
// 'B': [{name: 'Abby', score: 84}]
// // ...,
// 'F': [{name: 'Eddy', score: 58}]
// } groupWith Added in v0.21.0
((a, a) → Boolean) → [a] → [[a]]
Parameters
-
fnFunction for determining whether two given (adjacent) elements should be in the same group
-
listThe array to group. Also accepts a string, which will be treated as a list of characters.
Returns
- List A list that contains sublists of elements, whose concatenations are equal to the original list.
Takes a list and returns a list of lists where each sublist's elements are all satisfied pairwise comparison according to the provided function. Only adjacent elements are passed to the comparison function.
R.groupWith(R.equals, [0, 1, 1, 2, 3, 5, 8, 13, 21]) //=> [[0], [1, 1], [2], [3], [5], [8], [13], [21]] R.groupWith((a, b) => a + 1 === b, [0, 1, 1, 2, 3, 5, 8, 13, 21]) //=> [[0, 1], [1, 2, 3], [5], [8], [13], [21]] R.groupWith((a, b) => a % 2 === b % 2, [0, 1, 1, 2, 3, 5, 8, 13, 21]) //=> [[0], [1, 1], [2], [3, 5], [8], [13, 21]] const isVowel = R.test(/^[aeiou]$/i); R.groupWith(R.eqBy(isVowel), 'aestiou') //=> ['ae', 'st', 'iou']
gt Added in v0.1.0
Ord a => a → a → Boolean
Parameters
-
a -
b
Returns
- Boolean
Returns true if the first argument is greater than the second; false otherwise.
See also lt.
R.gt(2, 1); //=> true
R.gt(2, 2); //=> false
R.gt(2, 3); //=> false
R.gt('a', 'z'); //=> false
R.gt('z', 'a'); //=> true gte Added in v0.1.0
Ord a => a → a → Boolean
Parameters
-
a -
b
Returns
- Boolean
Returns true if the first argument is greater than or equal to the second; false otherwise.
See also lte.
R.gte(2, 1); //=> true
R.gte(2, 2); //=> true
R.gte(2, 3); //=> false
R.gte('a', 'z'); //=> false
R.gte('z', 'a'); //=> true has Added in v0.7.0
s → {s: x} → Boolean Parameters
-
propThe name of the property to check for.
-
objThe object to query.
Returns
- Boolean Whether the property exists.
Returns whether or not an object has an own property with the specified name
const hasName = R.has('name');
hasName({name: 'alice'}); //=> true
hasName({name: 'bob'}); //=> true
hasName({}); //=> false
const point = {x: 0, y: 0};
const pointHas = R.has(R.__, point);
pointHas('x'); //=> true
pointHas('y'); //=> true
pointHas('z'); //=> false hasIn Added in v0.7.0
s → {s: x} → Boolean Parameters
-
propThe name of the property to check for.
-
objThe object to query.
Returns
- Boolean Whether the property exists.
Returns whether or not an object or its prototype chain has a property with the specified name
function Rectangle(width, height) {
this.width = width;
this.height = height;
}
Rectangle.prototype.area = function() {
return this.width * this.height;
};
const square = new Rectangle(2, 2);
R.hasIn('width', square); //=> true
R.hasIn('area', square); //=> true hasPath Added in v0.26.0
[Idx] → {a} → Boolean Idx = String | Int | SymbolParameters
-
pathThe path to use.
-
objThe object to check the path in.
Returns
- Boolean Whether the path exists.
Returns whether or not a path exists in an object. Only the object's own properties are checked.
See also has.
R.hasPath(['a', 'b'], {a: {b: 2}}); // => true
R.hasPath(['a', 'b'], {a: {b: undefined}}); // => true
R.hasPath(['a', 'b'], {a: {c: 2}}); // => false
R.hasPath(['a', 'b'], {}); // => false head Added in v0.1.0
[a] → a | Undefined
String → String | UndefinedParameters
-
list
Returns
- *
Returns the first element of the given list or string. In some libraries this function is named first.
R.head(['fi', 'fo', 'fum']); //=> 'fi'
R.head([]); //=> undefined
R.head('abc'); //=> 'a'
R.head(''); //=> undefined identical Added in v0.15.0
a → a → Boolean
Parameters
-
a -
b
Returns
- Boolean
Returns true if its arguments are identical, false otherwise. Values are identical if they reference the same memory. NaN is identical to NaN; 0 and -0 are not identical.
Note this is merely a curried version of ES6 Object.is.
identical does not support the __ placeholder.
const o = {};
R.identical(o, o); //=> true
R.identical(1, 1); //=> true
R.identical(1, '1'); //=> false
R.identical([], []); //=> false
R.identical(0, -0); //=> false
R.identical(NaN, NaN); //=> true identity Added in v0.1.0
a → a
Parameters
-
xThe value to return.
Returns
- * The input value, `x`.
A function that does nothing but return the parameter supplied to it. Good as a default or placeholder function.
R.identity(1); //=> 1
const obj = {};
R.identity(obj) === obj; //=> true ifElse Added in v0.8.0
(*… → Boolean) → (*… → *) → (*… → *) → (*… → *)
Parameters
-
conditionA predicate function
-
onTrueA function to invoke when the
conditionevaluates to a truthy value. -
onFalseA function to invoke when the
conditionevaluates to a falsy value.
Returns
- function A new function that will process either the `onTrue` or the `onFalse` function depending upon the result of the `condition` predicate.
Creates a function that will process either the onTrue or the onFalse function depending upon the result of the condition predicate.
Note that ifElse takes its arity from the longest of the three functions passed to it.
const incCount = R.ifElse(
R.has('count'),
R.over(R.lensProp('count'), R.inc),
R.assoc('count', 1)
);
incCount({ count: 1 }); //=> { count: 2 }
incCount({}); //=> { count: 1 } inc Added in v0.9.0
Number → Number
Parameters
-
n
Returns
- Number n + 1
Increments its argument.
See also dec.
R.inc(42); //=> 43
includes Added in v0.26.0
a → [a] → Boolean
Parameters
-
aThe item to compare against.
-
listThe array to consider.
Returns
- Boolean `true` if an equivalent item is in the list, `false` otherwise.
Returns true if the specified value is equal, in R.equals terms, to at least one element of the given list; false otherwise. Also works with strings.
See also any.
R.includes(3, [1, 2, 3]); //=> true
R.includes(4, [1, 2, 3]); //=> false
R.includes({ name: 'Fred' }, [{ name: 'Fred' }]); //=> true
R.includes([42], [[42]]); //=> true
R.includes('ba', 'banana'); //=>true indexBy Added in v0.19.0
Idx a => (b → a) → [b] → {a: b} Idx = String | Int | SymbolParameters
-
fnFunction :: a -> Idx
-
arrayThe array of objects to index
Returns
- Object An object indexing each array element by the given property.
Given a function that generates a key, turns a list of objects into an object indexing the objects by the given key. Note that if multiple objects generate the same value for the indexing key only the last value will be included in the generated object.
Acts as a transducer if a transformer is given in list position.
See also groupBy.
const list = [{id: 'xyz', title: 'A'}, {id: 'abc', title: 'B'}];
R.indexBy(R.prop('id'), list);
//=> {abc: {id: 'abc', title: 'B'}, xyz: {id: 'xyz', title: 'A'}} indexOf Added in v0.1.0
a → [a] → Number
Parameters
-
targetThe item to find.
-
xsThe array to search in.
Returns
- Number the index of the target, or -1 if the target is not found.
Returns the position of the first occurrence of an item in an array, or -1 if the item is not included in the array. R.equals is used to determine equality.
See also lastIndexOf, findIndex.
R.indexOf(3, [1,2,3,4]); //=> 2 R.indexOf(10, [1,2,3,4]); //=> -1
init Added in v0.9.0
[a] → [a]
String → StringParameters
-
list
Returns
- *
Returns all but the last element of the given list or string.
R.init([1, 2, 3]); //=> [1, 2]
R.init([1, 2]); //=> [1]
R.init([1]); //=> []
R.init([]); //=> []
R.init('abc'); //=> 'ab'
R.init('ab'); //=> 'a'
R.init('a'); //=> ''
R.init(''); //=> '' innerJoin Added in v0.24.0
((a, b) → Boolean) → [a] → [b] → [a]
Parameters
-
pred -
xs -
ys
Returns
- Array
Takes a predicate pred, a list xs, and a list ys, and returns a list xs' comprising each of the elements of xs which is equal to one or more elements of ys according to pred.
pred must be a binary function expecting an element from each list.
xs, ys, and xs' are treated as sets, semantically, so ordering should not be significant, but since xs' is ordered the implementation guarantees that its values are in the same order as they appear in xs. Duplicates are not removed, so xs' may contain duplicates if xs contains duplicates.
See also intersection.
R.innerJoin(
(record, id) => record.id === id,
[{id: 824, name: 'Richie Furay'},
{id: 956, name: 'Dewey Martin'},
{id: 313, name: 'Bruce Palmer'},
{id: 456, name: 'Stephen Stills'},
{id: 177, name: 'Neil Young'}],
[177, 456, 999]
);
//=> [{id: 456, name: 'Stephen Stills'}, {id: 177, name: 'Neil Young'}] insert Added in v0.2.2
Number → a → [a] → [a]
Parameters
-
indexThe position to insert the element
-
eltThe element to insert into the Array
-
listThe list to insert into
Returns
- Array A new Array with `elt` inserted at `index`.
Inserts the supplied element into the list, at the specified index. Note that this is not destructive: it returns a copy of the list with the changes. No lists have been harmed in the application of this function.
R.insert(2, 'x', [1,2,3,4]); //=> [1,2,'x',3,4]
insertAll Added in v0.9.0
Number → [a] → [a] → [a]
Parameters
-
indexThe position to insert the sub-list
-
eltsThe sub-list to insert into the Array
-
listThe list to insert the sub-list into
Returns
- Array A new Array with `elts` inserted starting at `index`.
Inserts the sub-list into the list, at the specified index. Note that this is not destructive: it returns a copy of the list with the changes. No lists have been harmed in the application of this function.
R.insertAll(2, ['x','y','z'], [1,2,3,4]); //=> [1,2,'x','y','z',3,4]
intersection Added in v0.1.0
[*] → [*] → [*]
Parameters
-
list1The first list.
-
list2The second list.
Returns
- Array The list of elements found in both `list1` and `list2`.
Combines two lists into a set (i.e. no duplicates) composed of those elements common to both lists.
See also innerJoin.
R.intersection([1,2,3,4], [7,6,5,4,3]); //=> [4, 3]
intersperse Added in v0.14.0
a → [a] → [a]
Parameters
-
separatorThe element to add to the list.
-
listThe list to be interposed.
Returns
- Array The new list.
Creates a new list with the separator interposed between elements.
Dispatches to the intersperse method of the second argument, if present.
R.intersperse('a', ['b', 'n', 'n', 's']); //=> ['b', 'a', 'n', 'a', 'n', 'a', 's'] into Added in v0.12.0
a → (b → b) → [c] → a
Parameters
-
accThe initial accumulator value.
-
xfThe transducer function. Receives a transformer and returns a transformer.
-
listThe list to iterate over.
Returns
- * The final, accumulated value.
Transforms the items of the list with the transducer and appends the transformed items to the accumulator using an appropriate iterator function based on the accumulator type.
The accumulator can be an array, string, object or a transformer. Iterated items will be appended to arrays and concatenated to strings. Objects will be merged directly or 2-item arrays will be merged as key, value pairs.
The accumulator can also be a transformer 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 is used to provide the initial accumulator.
The iteration is performed with R.reduce after initializing the transducer.
See also transduce.
const numbers = [1, 2, 3, 4]; const transducer = R.compose(R.map(R.add(1)), R.take(2)); R.into([], transducer, numbers); //=> [2, 3] const intoArray = R.into([]); intoArray(transducer, numbers); //=> [2, 3]
invert Added in v0.9.0
{s: x} → {x: [ s, … ]} Parameters
-
objThe object or array to invert
Returns
- Object out A new object with keys in an array.
Same as R.invertObj, however this accounts for objects with duplicate values by putting the values into an array.
See also invertObj.
const raceResultsByFirstName = {
first: 'alice',
second: 'jake',
third: 'alice',
};
R.invert(raceResultsByFirstName);
//=> { 'alice': ['first', 'third'], 'jake':['second'] } invertObj Added in v0.9.0
{s: x} → {x: s} Parameters
-
objThe object or array to invert
Returns
- Object out A new object
Returns a new object with the keys of the given object as values, and the values of the given object, which are coerced to strings, as keys. Note that the last key found is preferred when handling the same value.
See also invert.
const raceResults = {
first: 'alice',
second: 'jake'
};
R.invertObj(raceResults);
//=> { 'alice': 'first', 'jake':'second' }
// Alternatively:
const raceResults = ['alice', 'jake'];
R.invertObj(raceResults);
//=> { 'alice': '0', 'jake':'1' } invoker Added in v0.1.0
Number → String → (a → b → … → n → Object → *)
Parameters
-
arityNumber of arguments the returned function should take before the target object.
-
methodName of any of the target object's methods to call.
Returns
- function A new curried function.
Given an arity (Number) and a name (String) the invoker function returns a curried function that takes arity arguments and a context object. It will "invoke" the name'd function (a method) on the context object.
See also construct.
// A function with no arguments
const asJson = invoker(0, "json")
// Just like calling .then((response) => response.json())
fetch("http://example.com/index.json").then(asJson)
// A function with one argument
const sliceFrom = invoker(1, 'slice');
sliceFrom(6, 'abcdefghijklm'); //=> 'ghijklm'
// A function with two arguments
const sliceFrom6 = invoker(2, 'slice')(6);
sliceFrom6(8, 'abcdefghijklm'); //=> 'gh'
// NOTE: You can't simply pass some of the arguments to the initial invoker function.
const firstCreditCardSection = invoker(2, "slice", 0, 4)
firstCreditCardSection("4242 4242 4242 4242") // => Function<...>
// Since invoker returns a curried function, you may partially apply it to create the function you need.
const firstCreditCardSection = invoker(2, "slice")(0, 4)
firstCreditCardSection("4242 4242 4242 4242") // => "4242" is Added in v0.3.0
(* → {*}) → a → Boolean Parameters
-
ctorA constructor
-
valThe value to test
Returns
- Boolean
See if an object (i.e. val) is an instance of the supplied constructor. This function will check up the inheritance chain, if any. If val was created using Object.create, R.is(Object, val) === true.
R.is(Object, {}); //=> true
R.is(Number, 1); //=> true
R.is(Object, 1); //=> false
R.is(String, 's'); //=> true
R.is(String, new String('')); //=> true
R.is(Object, new String('')); //=> true
R.is(Object, 's'); //=> false
R.is(Number, {}); //=> false isEmpty Added in v0.1.0
a → Boolean
Parameters
-
x
Returns
- Boolean
Returns true if the given value is its type's empty value; false otherwise.
See also empty, isNotEmpty.
R.isEmpty([1, 2, 3]); //=> false
R.isEmpty([]); //=> true
R.isEmpty(''); //=> true
R.isEmpty(null); //=> false
R.isEmpty({}); //=> true
R.isEmpty({length: 0}); //=> false
R.isEmpty(Uint8Array.from('')); //=> true
R.isEmpty(new Set()) //=> true
R.isEmpty(new Map()) //=> true isNil Added in v0.9.0
* → Boolean
Parameters
-
xThe value to test.
Returns
- Boolean `true` if `x` is `undefined` or `null`, otherwise `false`.
Checks if the input value is null or undefined.
R.isNil(null); //=> true R.isNil(undefined); //=> true R.isNil(0); //=> false R.isNil([]); //=> false
isNotEmpty Added in v0.29.2
a → Boolean
Parameters
-
x
Returns
- Boolean
Returns false if the given value is its type's empty value; true otherwise.
R.isNotEmpty([1, 2, 3]); //=> true
R.isNotEmpty([]); //=> false
R.isNotEmpty(''); //=> false
R.isNotEmpty(null); //=> true
R.isNotEmpty({}); //=> false
R.isNotEmpty({length: 0}); //=> true
R.isNotEmpty(Uint8Array.from('')); //=> false isNotNil Added in v0.29.0
* → Boolean
Parameters
-
xThe value to test.
Returns
- Boolean `true` if `x` is not `undefined` or not `null`, otherwise `false`.
Checks if the input value is not null and not undefined.
R.isNotNil(null); //=> false R.isNotNil(undefined); //=> false R.isNotNil(0); //=> true R.isNotNil([]); //=> true
join Added in v0.1.0
String → [a] → String
Parameters
-
separatorThe string used to separate the elements.
-
xsThe elements to join into a string.
Returns
- String str The string made by concatenating `xs` with `separator`.
Returns a string made by inserting the separator between each element and concatenating all the elements into a single string.
See also split.
const spacer = R.join(' ');
spacer(['a', 2, 3.4]); //=> 'a 2 3.4'
R.join('|', [1, 2, 3]); //=> '1|2|3' juxt Added in v0.19.0
[(a, b, …, m) → n] → ((a, b, …, m) → [n])
Parameters
-
fnsAn array of functions
Returns
- function A function that returns a list of values after applying each of the original `fns` to its parameters.
juxt applies a list of functions to a list of values.
See also applySpec.
const getRange = R.juxt([Math.min, Math.max]); getRange(3, 4, 9, -3); //=> [-3, 9]
keys Added in v0.1.0
{k: v} → [k] Parameters
-
objThe object to extract properties from
Returns
- Array An array of the object's own properties.
Returns a list containing the names of all the enumerable own properties of the supplied object. Note that the order of the output array is not guaranteed to be consistent across different JS platforms.
See also keysIn, values, toPairs.
R.keys({a: 1, b: 2, c: 3}); //=> ['a', 'b', 'c'] keysIn Added in v0.2.0
{k: v} → [k] Parameters
-
objThe object to extract properties from
Returns
- Array An array of the object's own and prototype properties.
Returns a list containing the names of all the properties of the supplied object, including prototype properties. Note that the order of the output array is not guaranteed to be consistent across different JS platforms.
const F = function() { this.x = 'X'; };
F.prototype.y = 'Y';
const f = new F();
R.keysIn(f); //=> ['x', 'y'] last Added in v0.1.4
[a] → a | Undefined
String → String | UndefinedParameters
-
list
Returns
- *
Returns the last element of the given list or string.
R.last(['fi', 'fo', 'fum']); //=> 'fum'
R.last([]); //=> undefined
R.last('abc'); //=> 'c'
R.last(''); //=> undefined lastIndexOf Added in v0.1.0
a → [a] → Number
Parameters
-
targetThe item to find.
-
xsThe array to search in.
Returns
- Number the index of the target, or -1 if the target is not found.
Returns the position of the last occurrence of an item in an array, or -1 if the item is not included in the array. R.equals is used to determine equality.
See also indexOf, findLastIndex.
R.lastIndexOf(3, [-1,3,3,0,1,2,3,4]); //=> 6 R.lastIndexOf(10, [1,2,3,4]); //=> -1
length Added in v0.3.0
[a] → Number
Parameters
-
listThe array to inspect.
Returns
- Number The length of the array.
Returns the number of elements in the array by returning list.length.
R.length([]); //=> 0 R.length([1, 2, 3]); //=> 3
lens Added in v0.8.0
(s → a) → ((a, s) → s) → Lens s a
Lens s a = Functor f => (a → f a) → s → f sParameters
-
getter -
setter
Returns
- Lens
Returns a lens for the given getter and setter functions. The getter "gets" the value of the focus; the setter "sets" the value of the focus. The setter should not mutate the data structure.
See also view, set, over, lensIndex, lensProp.
const xLens = R.lens(R.prop('x'), R.assoc('x'));
R.view(xLens, {x: 1, y: 2}); //=> 1
R.set(xLens, 4, {x: 1, y: 2}); //=> {x: 4, y: 2}
R.over(xLens, R.negate, {x: 1, y: 2}); //=> {x: -1, y: 2} lensIndex Added in v0.14.0
Number → Lens s a
Lens s a = Functor f => (a → f a) → s → f sParameters
-
n
Returns
- Lens
Returns a lens whose focus is the specified index.
When idx < -list.length || idx >= list.length, R.set or R.over, the original list is returned.
See also view, set, over, nth.
const headLens = R.lensIndex(0); R.view(headLens, ['a', 'b', 'c']); //=> 'a' R.set(headLens, 'x', ['a', 'b', 'c']); //=> ['x', 'b', 'c'] R.over(headLens, R.toUpper, ['a', 'b', 'c']); //=> ['A', 'b', 'c'] // out-of-range returns original list R.set(R.lensIndex(3), 'x', ['a', 'b', 'c']); //=> ['a', 'b', 'c'] R.over(R.lensIndex(-4), R.toUpper, ['a', 'b', 'c']); //=> ['a', 'b', 'c']
lensPath Added in v0.19.0
[Idx] → Lens s a
Idx = String | Int | SymbolLens s a = Functor f => (a → f a) → s → f sParameters
-
pathThe path to use.
Returns
- Lens
Returns a lens whose focus is the specified path.
const xHeadYLens = R.lensPath(['x', 0, 'y']);
R.view(xHeadYLens, {x: [{y: 2, z: 3}, {y: 4, z: 5}]});
//=> 2
R.set(xHeadYLens, 1, {x: [{y: 2, z: 3}, {y: 4, z: 5}]});
//=> {x: [{y: 1, z: 3}, {y: 4, z: 5}]}
R.over(xHeadYLens, R.negate, {x: [{y: 2, z: 3}, {y: 4, z: 5}]});
//=> {x: [{y: -2, z: 3}, {y: 4, z: 5}]} lensProp Added in v0.14.0
String → Lens s a
Lens s a = Functor f => (a → f a) → s → f sParameters
-
k
Returns
- Lens
Returns a lens whose focus is the specified property.
const xLens = R.lensProp('x');
R.view(xLens, {x: 1, y: 2}); //=> 1
R.set(xLens, 4, {x: 1, y: 2}); //=> {x: 4, y: 2}
R.over(xLens, R.negate, {x: 1, y: 2}); //=> {x: -1, y: 2} lift Added in v0.7.0
(*… → *) → ([*]… → [*])
Parameters
-
fnThe function to lift into higher context
Returns
- function The lifted function.
"lifts" a function of arity >= 1 so that it may "map over" a list, Function or other object that satisfies the FantasyLand Apply spec.
See also liftN.
const madd3 = R.lift((a, b, c) => a + b + c); madd3([100, 200], [30, 40], [5, 6, 7]); //=> [135, 136, 137, 145, 146, 147, 235, 236, 237, 245, 246, 247] const madd5 = R.lift((a, b, c, d, e) => a + b + c + d + e); madd5([10, 20], [1], [2, 3], [4], [100, 200]); //=> [117, 217, 118, 218, 127, 227, 128, 228]
liftN Added in v0.7.0
Number → (*… → *) → ([*]… → [*])
Parameters
-
fnThe function to lift into higher context
Returns
- function The lifted function.
"lifts" a function to be the specified arity, so that it may "map over" that many lists, Functions or other objects that satisfy the FantasyLand Apply spec.
const madd3 = R.liftN(3, (...args) => R.sum(args)); madd3([1,2,3], [1,2,3], [1]); //=> [3, 4, 5, 4, 5, 6, 5, 6, 7]
lt Added in v0.1.0
Ord a => a → a → Boolean
Parameters
-
a -
b
Returns
- Boolean
Returns true if the first argument is less than the second; false otherwise.
See also gt.
R.lt(2, 1); //=> false
R.lt(2, 2); //=> false
R.lt(2, 3); //=> true
R.lt('a', 'z'); //=> true
R.lt('z', 'a'); //=> false lte Added in v0.1.0
Ord a => a → a → Boolean
Parameters
-
a -
b
Returns
- Boolean
Returns true if the first argument is less than or equal to the second; false otherwise.
See also gte.
R.lte(2, 1); //=> false
R.lte(2, 2); //=> true
R.lte(2, 3); //=> true
R.lte('a', 'z'); //=> true
R.lte('z', 'a'); //=> false map Added in v0.1.0
Functor f => (a → b) → f a → f b
Parameters
-
fnThe function to be called on every element of the input
list. -
listThe list to be iterated over.
Returns
- Array The new list.
Takes a function and a functor, applies the function to each of the functor's values, and returns a functor of the same shape.
Ramda provides suitable map implementations for Array and Object, so this function may be applied to [1, 2, 3] or {x: 1, y: 2, z: 3}.
Dispatches to the map method of the second argument, if present.
Acts as a transducer if a transformer is given in list position.
Also treats functions as functors and will compose them together.
See also transduce, addIndex, pluck, project.
const double = x => x * 2;
R.map(double, [1, 2, 3]); //=> [2, 4, 6]
R.map(double, {x: 1, y: 2, z: 3}); //=> {x: 2, y: 4, z: 6} mapAccum Added in v0.10.0
((acc, x) → (acc, y)) → acc → [x] → (acc, [y])
Parameters
-
fnThe function to be called on every element of the input
list. -
accThe accumulator value.
-
listThe list to iterate over.
Returns
- * The final, accumulated value.
The mapAccum function behaves like a combination of map and reduce; it applies a function to each element of a list, passing an accumulating parameter from left to right, and returning a final value of this accumulator together with the new list.
The iterator function receives two arguments, acc and value, and should return a tuple [acc, value].
See also scan, addIndex, mapAccumRight.
const digits = ['1', '2', '3', '4']; const appender = (a, b) => [a + b, a + b]; R.mapAccum(appender, 0, digits); //=> ['01234', ['01', '012', '0123', '01234']]
mapAccumRight Added in v0.10.0
((acc, x) → (acc, y)) → acc → [x] → (acc, [y])
Parameters
-
fnThe function to be called on every element of the input
list. -
accThe accumulator value.
-
listThe list to iterate over.
Returns
- * The final, accumulated value.
The mapAccumRight function behaves like a combination of map and reduce; it applies a function to each element of a list, passing an accumulating parameter from right to left, and returning a final value of this accumulator together with the new list.
Similar to mapAccum, except moves through the input list from the right to the left.
The iterator function receives two arguments, acc and value, and should return a tuple [acc, value].
const digits = ['1', '2', '3', '4']; const appender = (a, b) => [b + a, b + a]; R.mapAccumRight(appender, 5, digits); //=> ['12345', ['12345', '2345', '345', '45']]
mapKeys Added in v0.31.0
(String → String) → Object → Object
Parameters
-
fn -
obj
Returns
- Object
Transforms an object by converting the keys to new values.
Note that if multiple keys map to the same new key, the last one processed will dominate.
See also map, rebuild, renameKeys.
R.mapKeys(toUpper, {foo: 1, bar: 2, baz: 3}) //=> {FOO: 1, BAR: 2, BAZ: 3} mapObjIndexed Added in v0.9.0
((*, String, Object) → *) → Object → Object
Parameters
-
fn -
obj
Returns
- Object
An Object-specific version of map. The function is applied to three arguments: (value, key, obj). If only the value is significant, use map instead.
See also map.
const xyz = { x: 1, y: 2, z: 3 };
const prependKeyAndDouble = (num, key, obj) => key + (num * 2);
R.mapObjIndexed(prependKeyAndDouble, xyz); //=> { x: 'x2', y: 'y4', z: 'z6' } match Added in v0.1.0
RegExp → String → [String | Undefined]
Parameters
-
rxA regular expression.
-
strThe string to match against
Returns
- Array The list of matches or empty array.
Tests a regular expression against a String. Note that this function will return an empty array when there are no matches. This differs from String.prototype.match which returns null when there are no matches.
See also test.
R.match(/([a-z]a)/g, 'bananas'); //=> ['ba', 'na', 'na'] R.match(/a/, 'b'); //=> [] R.match(/a/, null); //=> TypeError: null does not have a method named "match"
mathMod Added in v0.3.0
Number → Number → Number
Parameters
-
mThe dividend.
-
pthe modulus.
Returns
- Number The result of `b mod a`.
mathMod behaves like the modulo operator should mathematically, unlike the % operator (and by extension, R.modulo). So while -17 % 5 is -2, mathMod(-17, 5) is 3. mathMod requires Integer arguments, and returns NaN when the modulus is zero or negative.
See also modulo.
R.mathMod(-17, 5); //=> 3 R.mathMod(17, 5); //=> 2 R.mathMod(17, -5); //=> NaN R.mathMod(17, 0); //=> NaN R.mathMod(17.2, 5); //=> NaN R.mathMod(17, 5.3); //=> NaN const clock = R.mathMod(R.__, 12); clock(15); //=> 3 clock(24); //=> 0 const seventeenMod = R.mathMod(17); seventeenMod(3); //=> 2 seventeenMod(4); //=> 1 seventeenMod(10); //=> 7
max Added in v0.1.0
Ord a => a → a → a
Parameters
-
a -
b
Returns
- *
Returns the larger of its two arguments.
R.max(789, 123); //=> 789
R.max('a', 'b'); //=> 'b' maxBy Added in v0.8.0
Ord b => (a → b) → a → a → a
Parameters
-
f -
a -
b
Returns
- *
Takes a function and two values, and returns whichever value produces the larger result when passed to the provided function.
// square :: Number -> Number const square = n => n * n; R.maxBy(square, -3, 2); //=> -3 R.reduce(R.maxBy(square), 0, [3, -5, 4, 1, -2]); //=> -5 R.reduce(R.maxBy(square), 0, []); //=> 0
mean Added in v0.14.0
[Number] → Number
Parameters
-
list
Returns
- Number
Returns the mean of the given list of numbers.
See also median.
R.mean([2, 7, 9]); //=> 6 R.mean([]); //=> NaN
median Added in v0.14.0
[Number] → Number
Parameters
-
list
Returns
- Number
Returns the median of the given list of numbers.
See also mean.
R.median([2, 9, 7]); //=> 7 R.median([7, 2, 10, 9]); //=> 8 R.median([]); //=> NaN
memoizeWith Added in v0.24.0
(*… → String) → (*… → a) → (*… → a)
Parameters
-
keyGenThe function to generate the cache key.
-
fnThe function to memoize.
Returns
- function Memoized version of `fn`.
Takes a string-returning function keyGen and a function fn and returns a new function that returns cached results for subsequent calls with the same arguments.
When the function is invoked, keyGen is applied to the same arguments and its result becomes the cache key. If the cache contains something under that key, the function simply returns it and does not invoke fn at all.
Otherwise fn is applied to the same arguments and its return value is cached under that key and returned by the function.
Care must be taken when implementing keyGen to avoid key collision, or if tracking references, memory leaks and mutating arguments.
const withAge = memoizeWith(o => `${o.birth}/${o.death}`, ({birth, death}) => {
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^
// keyGen fn
console.log(`computing age for ${birth}/${death}`);
return ({birth, death, age: death - birth});
});
withAge({birth: 1921, death: 1999});
//=> LOG: computing age for 1921/1999
//=> {birth: 1921, death: 1999, age: 78} (returned from fn)
withAge({birth: 1921, death: 1999});
//=> {birth: 1921, death: 1999, age: 78} (returned from cache) mergeAll Added in v0.10.0
[{k: v}] → {k: v} Parameters
-
listAn array of objects
Returns
- Object A merged object.
Creates one new object with the own properties from a list of objects. If a key exists in more than one object, the value from the last object it exists in will be used.
See also reduce.
R.mergeAll([{foo:1},{bar:2},{baz:3}]); //=> {foo:1,bar:2,baz:3}
R.mergeAll([{foo:1},{foo:2},{bar:2}]); //=> {foo:2,bar:2} mergeDeepLeft Added in v0.24.0
{a} → {a} → {a} Parameters
-
lObj -
rObj
Returns
- Object
Creates a new object with the own properties of the first object merged with the own properties of the second object. If a key exists in both objects:
- and both values are objects, the two values will be recursively merged
- otherwise the value from the first object will be used.
See also mergeDeepRight, mergeDeepWith, mergeDeepWithKey.
R.mergeDeepLeft({ name: 'fred', age: 10, contact: { email: 'moo@example.com' }},
{ age: 40, contact: { email: 'baa@example.com' }});
//=> { name: 'fred', age: 10, contact: { email: 'moo@example.com' }} mergeDeepRight Added in v0.24.0
{a} → {a} → {a} Parameters
-
lObj -
rObj
Returns
- Object
Creates a new object with the own properties of the first object merged with the own properties of the second object. If a key exists in both objects:
- and both values are objects, the two values will be recursively merged
- otherwise the value from the second object will be used.
See also mergeDeepLeft, mergeDeepWith, mergeDeepWithKey.
R.mergeDeepRight({ name: 'fred', age: 10, contact: { email: 'moo@example.com' }},
{ age: 40, contact: { email: 'baa@example.com' }});
//=> { name: 'fred', age: 40, contact: { email: 'baa@example.com' }} mergeDeepWith Added in v0.24.0
((a, a) → a) → {a} → {a} → {a} Parameters
-
fn -
lObj -
rObj
Returns
- Object
Creates a new object with the own properties of the two provided objects. If a key exists in both objects:
- and both associated values are also objects then the values will be recursively merged.
- otherwise the provided function is applied to associated values using the resulting value as the new value associated with the key. If a key only exists in one object, the value will be associated with the key of the resulting object.
See also mergeWith, mergeDeepWithKey.
R.mergeDeepWith(R.concat,
{ a: true, c: { values: [10, 20] }},
{ b: true, c: { values: [15, 35] }});
//=> { a: true, b: true, c: { values: [10, 20, 15, 35] }} mergeDeepWithKey Added in v0.24.0
((String, a, a) → a) → {a} → {a} → {a} Parameters
-
fn -
lObj -
rObj
Returns
- Object
Creates a new object with the own properties of the two provided objects. If a key exists in both objects:
- and both associated values are also objects then the values will be recursively merged.
- otherwise the provided function is applied to the key and associated values using the resulting value as the new value associated with the key. If a key only exists in one object, the value will be associated with the key of the resulting object.
See also mergeWithKey, mergeDeepWith.
let concatValues = (k, l, r) => k == 'values' ? R.concat(l, r) : r
R.mergeDeepWithKey(concatValues,
{ a: true, c: { thing: 'foo', values: [10, 20] }},
{ b: true, c: { thing: 'bar', values: [15, 35] }});
//=> { a: true, b: true, c: { thing: 'bar', values: [10, 20, 15, 35] }} mergeLeft Added in v0.26.0
{k: v} → {k: v} → {k: v} Parameters
-
l -
r
Returns
- Object
Create a new object with the own properties of the first object merged with the own properties of the second object. If a key exists in both objects, the value from the first object will be used.
See also mergeRight, mergeDeepLeft, mergeWith, mergeWithKey.
R.mergeLeft({ 'age': 40 }, { 'name': 'fred', 'age': 10 });
//=> { 'name': 'fred', 'age': 40 }
const resetToDefault = R.mergeLeft({x: 0});
resetToDefault({x: 5, y: 2}); //=> {x: 0, y: 2} mergeRight Added in v0.26.0
{k: v} → {k: v} → {k: v} Parameters
-
l -
r
Returns
- Object
Create a new object with the own properties of the first object merged with the own properties of the second object. If a key exists in both objects, the value from the second object will be used.
See also mergeLeft, mergeDeepRight, mergeWith, mergeWithKey.
R.mergeRight({ 'name': 'fred', 'age': 10 }, { 'age': 40 });
//=> { 'name': 'fred', 'age': 40 }
const withDefaults = R.mergeRight({x: 0, y: 0});
withDefaults({y: 2}); //=> {x: 0, y: 2} mergeWith Added in v0.19.0
((a, a) → a) → {a} → {a} → {a} Parameters
-
fn -
l -
r
Returns
- Object
Creates a new object with the own properties of the two provided objects. If a key exists in both objects, the provided function is applied to the values associated with the key in each object, with the result being used as the value associated with the key in the returned object.
See also mergeDeepWith, mergeWithKey.
R.mergeWith(R.concat,
{ a: true, values: [10, 20] },
{ b: true, values: [15, 35] });
//=> { a: true, b: true, values: [10, 20, 15, 35] } mergeWithKey Added in v0.19.0
((String, a, a) → a) → {a} → {a} → {a} Parameters
-
fn -
l -
r
Returns
- Object
Creates a new object with the own properties of the two provided objects. If a key exists in both objects, the provided function is applied to the key and the values associated with the key in each object, with the result being used as the value associated with the key in the returned object.
See also mergeDeepWithKey, mergeWith.
let concatValues = (k, l, r) => k == 'values' ? R.concat(l, r) : r
R.mergeWithKey(concatValues,
{ a: true, thing: 'foo', values: [10, 20] },
{ b: true, thing: 'bar', values: [15, 35] });
//=> { a: true, b: true, thing: 'bar', values: [10, 20, 15, 35] } min Added in v0.1.0
Ord a => a → a → a
Parameters
-
a -
b
Returns
- *
Returns the smaller of its two arguments.
R.min(789, 123); //=> 123
R.min('a', 'b'); //=> 'a' minBy Added in v0.8.0
Ord b => (a → b) → a → a → a
Parameters
-
f -
a -
b
Returns
- *
Takes a function and two values, and returns whichever value produces the smaller result when passed to the provided function.
// square :: Number -> Number const square = n => n * n; R.minBy(square, -3, 2); //=> 2 R.reduce(R.minBy(square), Infinity, [3, -5, 4, 1, -2]); //=> 1 R.reduce(R.minBy(square), Infinity, []); //=> Infinity
modify Added in v0.28.0
Idx → (v → v) → {k: v} → {k: v} Parameters
-
propThe property to be modified.
-
fnThe function to apply to the property.
-
objectThe object to be transformed.
Returns
- Object The transformed object.
Creates a copy of the passed object by applying an fn function to the given prop property.
The function will not be invoked, and the object will not change if its corresponding property does not exist in the object. All non-primitive properties are copied to the new object by reference.
const person = {name: 'James', age: 20, pets: ['dog', 'cat']};
R.modify('age', R.add(1), person); //=> {name: 'James', age: 21, pets: ['dog', 'cat']}
R.modify('pets', R.append('turtle'), person); //=> {name: 'James', age: 20, pets: ['dog', 'cat', 'turtle']} modifyPath Added in v0.28.0
[Idx] → (v → v) → {k: v} → {k: v} Parameters
-
pathThe path to be modified.
-
fnThe function to apply to the path.
-
objectThe object to be transformed.
Returns
- Object The transformed object.
Creates a shallow clone of the passed object by applying an fn function to the value at the given path.
The function will not be invoked, and the object will not change if its corresponding path does not exist in the object. All non-primitive properties are copied to the new object by reference.
const person = {name: 'James', address: { zipCode: '90216' }};
R.modifyPath(['address', 'zipCode'], R.reverse, person); //=> {name: 'James', address: { zipCode: '61209' }}
// Can handle arrays too
const person = {name: 'James', addresses: [{ zipCode: '90216' }]};
R.modifyPath(['addresses', 0, 'zipCode'], R.reverse, person); //=> {name: 'James', addresses: [{ zipCode: '61209' }]} modulo Added in v0.1.1
Number → Number → Number
Parameters
-
aThe value to the divide.
-
bThe pseudo-modulus
Returns
- Number The result of `b % a`.
Divides the first parameter by the second and returns the remainder. Note that this function preserves the JavaScript-style behavior for modulo. For mathematical modulo see mathMod.
See also mathMod.
R.modulo(17, 3); //=> 2 // JS behavior: R.modulo(-17, 3); //=> -2 R.modulo(17, -3); //=> 2 const isOdd = R.modulo(R.__, 2); isOdd(42); //=> 0 isOdd(21); //=> 1
move Added in v0.27.1
Number → Number → [a] → [a]
Parameters
-
fromThe source index
-
toThe destination index
-
listThe list which will serve to realise the move
Returns
- Array The new list reordered
Move an item, at index from, to index to, in a list of elements. A new list will be created containing the new elements order.
R.move(0, 2, ['a', 'b', 'c', 'd', 'e', 'f']); //=> ['b', 'c', 'a', 'd', 'e', 'f'] R.move(-1, 0, ['a', 'b', 'c', 'd', 'e', 'f']); //=> ['f', 'a', 'b', 'c', 'd', 'e'] list rotation
multiply Added in v0.1.0
Number → Number → Number
Parameters
-
aThe first value.
-
bThe second value.
Returns
- Number The result of `a * b`.
Multiplies two numbers. Equivalent to a * b but curried.
See also divide.
const double = R.multiply(2); const triple = R.multiply(3); double(3); //=> 6 triple(4); //=> 12 R.multiply(2, 5); //=> 10
nAry Added in v0.1.0
Number → (* → a) → (* → a)
Parameters
-
nThe desired arity of the new function.
-
fnThe function to wrap.
Returns
- function A new function wrapping `fn`. The new function is guaranteed to be of arity `n`.
Wraps a function of any arity (including nullary) in a function that accepts exactly n parameters. Any extraneous parameters will not be passed to the supplied function.
const takesTwoArgs = (a, b) => [a, b]; takesTwoArgs.length; //=> 2 takesTwoArgs(1, 2); //=> [1, 2] const takesOneArg = R.nAry(1, takesTwoArgs); takesOneArg.length; //=> 1 // Only `n` arguments are passed to the wrapped function takesOneArg(1, 2); //=> [1, undefined]
negate Added in v0.9.0
Number → Number
Parameters
-
n
Returns
- Number
Negates its argument.
R.negate(42); //=> -42
none Added in v0.12.0
(a → Boolean) → [a] → Boolean
Parameters
-
fnThe predicate function.
-
listThe array to consider.
Returns
- Boolean `true` if the predicate is not satisfied by every element, `false` otherwise.
Returns true if no elements of the list match the predicate, false otherwise.
Dispatches to the all method of the second argument, if present.
Acts as a transducer if a transformer is given in list position.
const isEven = n => n % 2 === 0; const isOdd = n => n % 2 !== 0; R.none(isEven, [1, 3, 5, 7, 9, 11]); //=> true R.none(isOdd, [1, 3, 5, 7, 8, 11]); //=> false
not Added in v0.1.0
* → Boolean
Parameters
-
aany value
Returns
- Boolean the logical inverse of passed argument.
A function that returns the ! of its argument. It will return true when passed false-y value, and false when passed a truth-y one.
See also complement.
R.not(true); //=> false R.not(false); //=> true R.not(0); //=> true R.not(1); //=> false
nth Added in v0.1.0
Number → [a] → a | Undefined
Number → String → String | UndefinedParameters
-
offset -
list
Returns
- *
Returns the nth element of the given list or string. If n is negative the element at index length + n is returned.
const list = ['foo', 'bar', 'baz', 'quux']; R.nth(1, list); //=> 'bar' R.nth(-1, list); //=> 'quux' R.nth(-99, list); //=> undefined R.nth(2, 'abc'); //=> 'c' R.nth(3, 'abc'); //=> undefined
nthArg Added in v0.9.0
Number → *… → *
Parameters
-
n
Returns
- function
Returns a function which returns its nth argument.
R.nthArg(1)('a', 'b', 'c'); //=> 'b'
R.nthArg(-1)('a', 'b', 'c'); //=> 'c' o Added in v0.24.0
(b → c) → (a → b) → a → c
Parameters
-
f -
g
Returns
- function
o is a curried composition function that returns a unary function. Like compose, o performs right-to-left function composition. Unlike compose, the rightmost function passed to o will be invoked with only one argument. Also, unlike compose, o is limited to accepting only 2 unary functions. The name o was chosen because of its similarity to the mathematical composition operator ∘.
const classyGreeting = name => "The name's " + name.last + ", " + name.first + " " + name.last
const yellGreeting = R.o(R.toUpper, classyGreeting);
yellGreeting({first: 'James', last: 'Bond'}); //=> "THE NAME'S BOND, JAMES BOND"
R.o(R.multiply(10), R.add(10))(-4) //=> 60 objOf Added in v0.18.0
String → a → {String:a} Parameters
-
key -
val
Returns
- Object
Creates an object containing a single key:value pair.
See also pair.
const matchPhrases = R.compose(
R.objOf('must'),
R.map(R.objOf('match_phrase'))
);
matchPhrases(['foo', 'bar', 'baz']); //=> {must: [{match_phrase: 'foo'}, {match_phrase: 'bar'}, {match_phrase: 'baz'}]} of Added in v0.3.0
(* → {*}) → a → {a} Parameters
-
CtorA constructor
-
valany value
Returns
- * An instance of the `Ctor` wrapping `val`.
Given a constructor and a value, returns a new instance of that constructor containing the value.
Dispatches to the fantasy-land/of method of the constructor first (if present) or to the of method last (if present). When neither are present, wraps the value in an array.
Note this of is different from the ES6 of; See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of
R.of(Array, 42); //=> [42] R.of(Array, [42]); //=> [[42]] R.of(Maybe, 42); //=> Maybe.Just(42)
omit Added in v0.1.0
[String] → {String: *} → {String: *} Parameters
-
namesan array of String property names to omit from the new object
-
objThe object to copy from
Returns
- Object A new object with properties from `names` not on it.
Returns a partial copy of an object omitting the keys specified.
See also pick.
R.omit(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> {b: 2, c: 3} on Added in v0.28.0
((a, a) → b) → (c → a) → c → c → b
Parameters
-
fa binary function
-
ga unary function
-
aany value
-
bany value
Returns
- any The result of `f`
Takes a binary function f, a unary function g, and two values. Applies g to each value, then applies the result of each to f.
Also known as the P combinator.
const eqBy = R.on((a, b) => a === b);
eqBy(R.prop('a'), {b:0, a:1}, {a:1}) //=> true;
const containsInsensitive = R.on(R.includes, R.toLower);
containsInsensitive('o', 'FOO'); //=> true once Added in v0.1.0
(a… → b) → (a… → b)
Parameters
-
fnThe function to wrap in a call-only-once wrapper.
Returns
- function The wrapped function.
Accepts a function fn and returns a function that guards invocation of fn such that fn can only ever be called once, no matter how many times the returned function is invoked. The first value calculated is returned in subsequent invocations.
const addOneOnce = R.once(x => x + 1); addOneOnce(10); //=> 11 addOneOnce(addOneOnce(50)); //=> 11
or Added in v0.1.0
a → b → a | b
Parameters
-
a -
b
Returns
- Any
Returns the first argument if it is truthy, otherwise the second argument. Acts as the boolean or statement if both inputs are Booleans.
R.or(true, true); //=> true R.or(true, false); //=> true R.or(false, true); //=> true R.or(false, false); //=> false
otherwise Added in v0.26.0
(e → b) → (Promise e a) → (Promise e b)
(e → (Promise f b)) → (Promise e a) → (Promise f b)Parameters
-
onFailureThe function to apply. Can return a value or a promise of a value.
-
p
Returns
- Promise The result of calling `p.then(null, onFailure)`
Returns the result of applying the onFailure function to the value inside a failed promise. This is useful for handling rejected promises inside function compositions.
See also andThen.
const failedFetch = id => Promise.reject('bad ID');
const useDefault = () => ({ firstName: 'Bob', lastName: 'Loblaw' });
//recoverFromFailure :: String -> Promise ({ firstName, lastName })
const recoverFromFailure = R.pipe(
failedFetch,
R.otherwise(useDefault),
R.andThen(R.pick(['firstName', 'lastName'])),
);
recoverFromFailure(12345).then(console.log); over Added in v0.16.0
Lens s a → (a → a) → s → s
Lens s a = Functor f => (a → f a) → s → f sParameters
-
lens -
v -
x
Returns
- *
Returns the result of "setting" the portion of the given data structure focused by the given lens to the result of applying the given function to the focused value.
See also view, set, lens, lensIndex, lensProp, lensPath.
const headLens = R.lensIndex(0); R.over(headLens, R.toUpper, ['foo', 'bar', 'baz']); //=> ['FOO', 'bar', 'baz']
pair Added in v0.18.0
a → b → (a,b)
Parameters
-
fst -
snd
Returns
- Array
Takes two arguments, fst and snd, and returns [fst, snd].
R.pair('foo', 'bar'); //=> ['foo', 'bar'] partial Added in v0.10.0
((a, b, c, …, n) → x) → [a, b, c, …] → ((d, e, f, …, n) → x)
Parameters
-
f -
args
Returns
- function
Takes a function f and a list of arguments, and returns a function g. When applied, g returns the result of applying f to the arguments provided initially followed by the arguments provided to g.
See also partialRight, curry.
const multiply2 = (a, b) => a * b;
const double = R.partial(multiply2, [2]);
double(3); //=> 6
const greet = (salutation, title, firstName, lastName) =>
salutation + ', ' + title + ' ' + firstName + ' ' + lastName + '!';
const sayHello = R.partial(greet, ['Hello']);
const sayHelloToMs = R.partial(sayHello, ['Ms.']);
sayHelloToMs('Jane', 'Jones'); //=> 'Hello, Ms. Jane Jones!' partialObject Added in v0.28.0
(({ a, b, c, …, n }) → x) → { a, b, c, …} → ({ d, e, f, …, n } → x) Parameters
-
f -
props
Returns
- function
Takes a function f and an object, and returns a function g. When applied, g returns the result of applying f to the object provided initially merged deeply (right) with the object provided as an argument to g.
See also partial, partialRight, curry, mergeDeepRight.
const multiply2 = ({ a, b }) => a * b;
const double = R.partialObject(multiply2, { a: 2 });
double({ b: 2 }); //=> 4
const greet = ({ salutation, title, firstName, lastName }) =>
salutation + ', ' + title + ' ' + firstName + ' ' + lastName + '!';
const sayHello = R.partialObject(greet, { salutation: 'Hello' });
const sayHelloToMs = R.partialObject(sayHello, { title: 'Ms.' });
sayHelloToMs({ firstName: 'Jane', lastName: 'Jones' }); //=> 'Hello, Ms. Jane Jones!' partialRight Added in v0.10.0
((a, b, c, …, n) → x) → [d, e, f, …, n] → ((a, b, c, …) → x)
Parameters
-
f -
args
Returns
- function
Takes a function f and a list of arguments, and returns a function g. When applied, g returns the result of applying f to the arguments provided to g followed by the arguments provided initially.
See also partial.
const greet = (salutation, title, firstName, lastName) =>
salutation + ', ' + title + ' ' + firstName + ' ' + lastName + '!';
const greetMsJaneJones = R.partialRight(greet, ['Ms.', 'Jane', 'Jones']);
greetMsJaneJones('Hello'); //=> 'Hello, Ms. Jane Jones!' partition Added in v0.1.4
Filterable f => (a → Boolean) → f a → [f a, f a]
Parameters
-
predA predicate to determine which side the element belongs to.
-
filterablethe list (or other filterable) to partition.
Returns
- Array An array, containing first the subset of elements that satisfy the predicate, and second the subset of elements that do not satisfy.
Takes a predicate and a list or other Filterable object and returns the pair of filterable objects of the same type of elements which do and do not satisfy, the predicate, respectively. Filterable objects include plain objects or any object that has a filter method such as Array.
R.partition(R.includes('s'), ['sss', 'ttt', 'foo', 'bars']);
// => [ [ 'sss', 'bars' ], [ 'ttt', 'foo' ] ]
R.partition(R.includes('s'), { a: 'sss', b: 'ttt', foo: 'bars' });
// => [ { a: 'sss', foo: 'bars' }, { b: 'ttt' } ] path Added in v0.2.0
[Idx] → {a} → a | Undefined Idx = String | NonNegativeIntIdx = String | Int | SymbolParameters
-
pathThe path to use.
-
objThe object or array to retrieve the nested property from.
Returns
- * The data at `path`.
Retrieves the value at a given path. The nodes of the path can be arbitrary strings or non-negative integers. For anything else, the value is unspecified. Integer paths are meant to index arrays, strings are meant for objects.
See also prop, nth, assocPath, dissocPath.
R.path(['a', 'b'], {a: {b: 2}}); //=> 2
R.path(['a', 'b'], {c: {b: 2}}); //=> undefined
R.path(['a', 'b', 0], {a: {b: [1, 2, 3]}}); //=> 1
R.path(['a', 'b', -2], {a: {b: [1, 2, 3]}}); //=> 2
R.path([2], {'2': 2}); //=> 2
R.path([-2], {'-2': 'a'}); //=> undefined pathEq Added in v0.7.0
a → [Idx] → {a} → Boolean Idx = String | Int | SymbolParameters
-
valThe value to compare the nested property with
-
pathThe path of the nested property to use
-
objThe object to check the nested property in
Returns
- Boolean `true` if the value equals the nested object property, `false` otherwise.
Determines whether a nested path on an object has a specific value, in R.equals terms. Most likely used to filter a list.
See also whereEq, propEq, pathSatisfies, equals.
const user1 = { address: { zipCode: 90210 } };
const user2 = { address: { zipCode: 55555 } };
const user3 = { name: 'Bob' };
const users = [ user1, user2, user3 ];
const isFamous = R.pathEq(90210, ['address', 'zipCode']);
R.filter(isFamous, users); //=> [ user1 ] pathOr Added in v0.18.0
a → [Idx] → {a} → a Idx = String | Int | SymbolParameters
-
dThe default value.
-
pThe path to use.
-
objThe object to retrieve the nested property from.
Returns
- * The data at `path` of the supplied object or the default value.
If the given, non-null object has a value at the given path, returns the value at that path. Otherwise returns the provided default value.
R.pathOr('N/A', ['a', 'b'], {a: {b: 2}}); //=> 2
R.pathOr('N/A', ['a', 'b'], {c: {b: 2}}); //=> "N/A" paths Added in v0.27.1
[Idx] → {a} → [a | Undefined] Idx = [String | Int | Symbol]Parameters
-
pathsArrayThe array of paths to be fetched.
-
objThe object to retrieve the nested properties from.
Returns
- Array A list consisting of values at paths specified by "pathsArray".
Retrieves the values at given paths of an object.
See also path.
R.paths([['a', 'b'], ['p', 0, 'q']], {a: {b: 2}, p: [{q: 3}]}); //=> [2, 3]
R.paths([['a', 'b'], ['p', 'r']], {a: {b: 2}, p: [{q: 3}]}); //=> [2, undefined] pathSatisfies Added in v0.19.0
(a → Boolean) → [Idx] → {a} → Boolean Idx = String | Int | SymbolParameters
-
pred -
propPath -
obj
Returns
- Boolean
Returns true if the specified object property at given path satisfies the given predicate; false otherwise.
See also propSatisfies, path.
R.pathSatisfies(y => y > 0, ['x', 'y'], {x: {y: 2}}); //=> true
R.pathSatisfies(R.is(Object), [], {x: {y: 2}}); //=> true pick Added in v0.1.0
[k] → {k: v} → {k: v} Parameters
-
namesan array of String property names to copy onto a new object
-
objThe object to copy from
Returns
- Object A new object with only properties from `names` on it.
Returns a partial copy of an object containing only the keys specified. If the key does not exist, the property is ignored.
R.pick(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1, d: 4}
R.pick(['a', 'e', 'f'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1} pickAll Added in v0.1.0
[k] → {k: v} → {k: v} Parameters
-
namesan array of String property names to copy onto a new object
-
objThe object to copy from
Returns
- Object A new object with only properties from `names` on it.
Similar to pick except that this one includes a key: undefined pair for properties that don't exist.
See also pick.
R.pickAll(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1, d: 4}
R.pickAll(['a', 'e', 'f'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1, e: undefined, f: undefined} pickBy Added in v0.8.0
((v, k) → Boolean) → {k: v} → {k: v} Parameters
-
predA predicate to determine whether or not a key should be included on the output object.
-
objThe object to copy from
Returns
- Object A new object with only properties that satisfy `pred` on it.
Returns a partial copy of an object containing only the keys that satisfy the supplied predicate.
const isUpperCase = (val, key) => key.toUpperCase() === key;
R.pickBy(isUpperCase, {a: 1, b: 2, A: 3, B: 4}); //=> {A: 3, B: 4} pipe Added in v0.1.0
(((a, b, …, n) → o), (o → p), …, (x → y), (y → z)) → ((a, b, …, n) → z)
Parameters
-
functions
Returns
- function
Performs left-to-right function composition. The first argument may have any arity; the remaining arguments must be unary.
In some libraries this function is named sequence.
Note: The result of pipe is not automatically curried.
See also compose.
const f = R.pipe(Math.pow, R.negate, R.inc); f(3, 4); // -(3^4) + 1
pipeWith Added in v0.26.0
((* → *), [((a, b, …, n) → o), (o → p), …, (x → y), (y → z)]) → ((a, b, …, n) → z)
Parameters
-
transformerThe transforming function
-
functionsThe functions to pipe
Returns
- function
Performs left-to-right function composition using transforming function. The first function may have any arity; the remaining functions must be unary.
Note: The result of pipeWith is not automatically curried. Transforming function is not used on the first argument.
See also andThen, composeWith, pipe.
pluck Added in v0.1.0
Functor f => k → f {k: v} → f v Parameters
-
keyThe key name to pluck off of each object.
-
fThe array or functor to consider.
Returns
- Array The list of values for the given key.
Returns a new list by plucking the same named property off all objects in the list supplied.
pluck will work on any functor in addition to arrays, as it is equivalent to R.map(R.prop(k), f).
See also project, prop, props.
var getAges = R.pluck('age');
getAges([{name: 'fred', age: 29}, {name: 'wilma', age: 27}]); //=> [29, 27]
R.pluck(0, [[1, 2], [3, 4]]); //=> [1, 3]
R.pluck('val', {a: {val: 3}, b: {val: 5}}); //=> {a: 3, b: 5} prepend Added in v0.1.0
a → [a] → [a]
Parameters
-
elThe item to add to the head of the output list.
-
listThe array to add to the tail of the output list.
Returns
- Array A new array.
Returns a new list with the given element at the front, followed by the contents of the list.
See also append.
R.prepend('fee', ['fi', 'fo', 'fum']); //=> ['fee', 'fi', 'fo', 'fum'] product Added in v0.1.0
[Number] → Number
Parameters
-
listAn array of numbers
Returns
- Number The product of all the numbers in the list.
Multiplies together all the elements of a list.
See also reduce.
R.product([2,4,6,8,100,1]); //=> 38400
project Added in v0.1.0
[k] → [{k: v}] → [{k: v}] Parameters
-
propsThe property names to project
-
objsThe objects to query
Returns
- Array An array of objects with just the `props` properties.
Reasonable analog to SQL select statement.
const abby = {name: 'Abby', age: 7, hair: 'blond', grade: 2};
const fred = {name: 'Fred', age: 12, hair: 'brown', grade: 7};
const kids = [abby, fred];
R.project(['name', 'grade'], kids); //=> [{name: 'Abby', grade: 2}, {name: 'Fred', grade: 7}] promap Added in v0.28.0
(a → b) → (c → d) → (b → c) → (a → d)
Profunctor p => (a → b) → (c → d) → p b c → p a dParameters
-
fThe preprocessor function, a -> b
-
gThe postprocessor function, c -> d
-
profunctorThe profunctor instance to be promapped, e.g. b -> c
Returns
- Profunctor The new profunctor instance, e.g. a -> d
Takes two functions as pre- and post- processors respectively for a third function, i.e. promap(f, g, h)(x) === g(h(f(x))).
Dispatches to the promap method of the third argument, if present, according to the FantasyLand Profunctor spec.
Acts as a transducer if a transformer is given in profunctor position.
See also transduce.
const decodeChar = R.promap(s => s.charCodeAt(), String.fromCharCode, R.add(-8))
const decodeString = R.promap(R.split(''), R.join(''), R.map(decodeChar))
decodeString("ziuli") //=> "ramda" prop Added in v0.1.0
Idx → {s: a} → a | Undefined Idx = String | Int | SymbolParameters
-
pThe property name or array index
-
objThe object to query
Returns
- * The value at `obj.p`.
Returns a function that when supplied an object returns the indicated property of that object, if it exists.
See also path, props, pluck, project, nth.
R.prop('x', {x: 100}); //=> 100
R.prop('x', {}); //=> undefined
R.prop(0, [100]); //=> 100
R.compose(R.inc, R.prop('x'))({ x: 3 }) //=> 4 propEq Added in v0.1.0
a → String → Object → Boolean
Parameters
-
valThe value to compare the property with
-
namethe specified object property's key
-
objThe object to check the property in
Returns
- Boolean `true` if the value equals the specified object property, `false` otherwise.
Returns true if the specified object property is equal, in R.equals terms, to the given value; false otherwise. You can test multiple properties with R.whereEq, and test nested path property with R.pathEq.
See also whereEq, pathEq, propSatisfies, equals.
const abby = {name: 'Abby', age: 7, hair: 'blond'};
const fred = {name: 'Fred', age: 12, hair: 'brown'};
const rusty = {name: 'Rusty', age: 10, hair: 'brown'};
const alois = {name: 'Alois', age: 15, disposition: 'surly'};
const kids = [abby, fred, rusty, alois];
const hasBrownHair = R.propEq('brown', 'hair');
R.filter(hasBrownHair, kids); //=> [fred, rusty] propIs Added in v0.16.0
Type → String → Object → Boolean
Parameters
-
type -
name -
obj
Returns
- Boolean
Returns true if the specified object property is of the given type; false otherwise.
See also is, propSatisfies.
R.propIs(Number, 'x', {x: 1, y: 2}); //=> true
R.propIs(Number, 'x', {x: 'foo'}); //=> false
R.propIs(Number, 'x', {}); //=> false propOr Added in v0.6.0
a → String → Object → a
Parameters
-
valThe default value.
-
pThe name of the property to return.
-
objThe object to query.
Returns
- * The value of given property of the supplied object or the default value.
Return the specified property of the given non-null object if the property is present and it's value is not null, undefined or NaN.
Otherwise the first argument is returned.
const alice = {
name: 'ALICE',
age: 101
};
const favorite = R.prop('favoriteLibrary');
const favoriteWithDefault = R.propOr('Ramda', 'favoriteLibrary');
favorite(alice); //=> undefined
favoriteWithDefault(alice); //=> 'Ramda' props Added in v0.1.0
[k] → {k: v} → [v] Parameters
-
psThe property names to fetch
-
objThe object to query
Returns
- Array The corresponding values or partially applied function.
Acts as multiple prop: array of keys in, array of values out. Preserves order.
See also prop, pluck, project.
R.props(['x', 'y'], {x: 1, y: 2}); //=> [1, 2]
R.props(['c', 'a', 'b'], {b: 2, a: 1}); //=> [undefined, 1, 2]
const fullName = R.compose(R.join(' '), R.props(['first', 'last']));
fullName({last: 'Bullet-Tooth', age: 33, first: 'Tony'}); //=> 'Tony Bullet-Tooth' propSatisfies Added in v0.16.0
(a → Boolean) → String → {String: a} → Boolean Parameters
-
pred -
name -
obj
Returns
- Boolean
Returns true if the specified object property satisfies the given predicate; false otherwise. You can test multiple properties with R.where.
See also where, propEq, propIs.
R.propSatisfies(x => x > 0, 'x', {x: 1, y: 2}); //=> true range Added in v0.1.0
Number → Number → [Number]
Parameters
-
fromThe first number in the list.
-
toOne more than the last number in the list.
Returns
- Array The list of numbers in the set `[a, b)`.
Returns a list of numbers from from (inclusive) to to (exclusive).
R.range(1, 5); //=> [1, 2, 3, 4] R.range(50, 53); //=> [50, 51, 52]
rebuild Added in v0.31.0
([String, a] → [[String, b]]) → {k: a} → {k: b} Parameters
-
convertA function that converts a key and a value to an array of key-value arrays.
-
objThe structure to convert
Returns
- Array A new object whose key-value pairs are the result of applying the `convert` function to every key-value pair in `obj`.
Transforms an object into a new one, applying to every key-value pair a function creating zero, one, or many new key-value pairs, and combining the resulst into a single object.
See also map, mapKeys, renameKeys.
R.rebuild((k, v) => [[k.toUpperCase(), v * v]], {a: 1, b: 2, c: 3}) //=> {A: 1, B: 4, C: 9} reduce Added in v0.1.0
((a, b) → a) → a → [b] → a
Parameters
-
fnThe iterator function. Receives two values, the accumulator and the current element from the array.
-
accThe accumulator value.
-
listThe list to iterate over.
Returns
- * The final, accumulated value.
Returns a single item by iterating through the list, 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 two values: (acc, value). It may use R.reduced to shortcut the iteration.
The arguments' order of reduceRight's iterator function is (value, acc).
Note: R.reduce does not skip deleted or unassigned indices (sparse arrays), unlike the native Array.prototype.reduce method. For more details on this behavior, see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce#Description
Be cautious of mutating and returning the accumulator. If you reuse it across invocations, it will continue to accumulate onto the same value. The general recommendation is to always return a new value. If you can't do so for performance reasons, then be sure to reinitialize the accumulator on each invocation.
Dispatches to the reduce method of the third argument, if present. When doing so, it is up to the user to handle the R.reduced shortcuting, as this is not implemented by reduce.
See also reduced, addIndex, reduceRight.
R.reduce(R.subtract, 0, [1, 2, 3, 4]) // => ((((0 - 1) - 2) - 3) - 4) = -10 // - -10 // / \ / \ // - 4 -6 4 // / \ / \ // - 3 ==> -3 3 // / \ / \ // - 2 -1 2 // / \ / \ // 0 1 0 1
reduceBy Added in v0.20.0
((a, b) → a) → a → (b → String) → [b] → {String: a} Parameters
-
valueFnThe function that reduces the elements of each group to a single value. Receives two values, accumulator for a particular group and the current element.
-
accThe (initial) accumulator value for each group.
-
keyFnThe function that maps the list's element into a key.
-
listThe array to group.
Returns
- Object An object with the output of `keyFn` for keys, mapped to the output of `valueFn` for elements which produced that key when passed to `keyFn`.
Groups the elements of the list according to the result of calling the String-returning function keyFn on each element and reduces the elements of each group to a single value via the reducer function valueFn.
The value function receives two values: (acc, value). It may use R.reduced to short circuit the iteration.
This function is basically a more general groupBy function.
Acts as a transducer if a transformer is given in list position.
See also groupBy, reduce, reduced.
const groupNames = (acc, {name}) => acc.concat(name)
const toGrade = ({score}) =>
score < 65 ? 'F' :
score < 70 ? 'D' :
score < 80 ? 'C' :
score < 90 ? 'B' : 'A'
var students = [
{name: 'Abby', score: 83},
{name: 'Bart', score: 62},
{name: 'Curt', score: 88},
{name: 'Dora', score: 92},
]
reduceBy(groupNames, [], toGrade, students)
//=> {"A": ["Dora"], "B": ["Abby", "Curt"], "F": ["Bart"]} reduced Added in v0.15.0
a → *
Parameters
-
xThe final value of the reduce.
Returns
- * The wrapped value.
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.
This optimization is available to the below functions:
See also reduce, reduceWhile, reduceBy, reduceRight, transduce.
R.reduce( (acc, item) => item > 3 ? R.reduced(acc) : acc.concat(item), [], [1, 2, 3, 4, 5]) // [1, 2, 3]
reduceRight Added in v0.1.0
((a, b) → b) → b → [a] → b
Parameters
-
fnThe iterator function. Receives two values, the current element from the array and the accumulator.
-
accThe accumulator value.
-
listThe list to iterate over.
Returns
- * The final, accumulated value.
Returns a single item by iterating through the list, 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.
Similar to reduce, except moves through the input list from the right to the left.
The iterator function receives two values: (value, acc), while the arguments' order of reduce's iterator function is (acc, value). reduceRight may use reduced to short circuit the iteration.
Note: R.reduceRight does not skip deleted or unassigned indices (sparse arrays), unlike the native Array.prototype.reduceRight method. For more details on this behavior, see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight#Description
Be cautious of mutating and returning the accumulator. If you reuse it across invocations, it will continue to accumulate onto the same value. The general recommendation is to always return a new value. If you can't do so for performance reasons, then be sure to reinitialize the accumulator on each invocation.
See also reduce, addIndex, reduced.
R.reduceRight(R.subtract, 0, [1, 2, 3, 4]) // => (1 - (2 - (3 - (4 - 0)))) = -2 // - -2 // / \ / \ // 1 - 1 3 // / \ / \ // 2 - ==> 2 -1 // / \ / \ // 3 - 3 4 // / \ / \ // 4 0 4 0
reduceWhile Added in v0.22.0
((a, b) → Boolean) → ((a, b) → a) → a → [b] → a
Parameters
-
predThe predicate. It is passed the accumulator and the current element.
-
fnThe iterator function. Receives two values, the accumulator and the current element.
-
aThe accumulator value.
-
listThe list to iterate over.
Returns
- * The final, accumulated value.
Like reduce, reduceWhile returns a single item by iterating through the list, successively calling the iterator function. reduceWhile also takes a predicate that is evaluated before each step. If the predicate returns false, it "short-circuits" the iteration and returns the current value of the accumulator. reduceWhile may alternatively be short-circuited via reduced.
const isOdd = (acc, x) => x % 2 !== 0; const xs = [1, 3, 5, 60, 777, 800]; R.reduceWhile(isOdd, R.add, 0, xs); //=> 9 const ys = [2, 4, 6] R.reduceWhile(isOdd, R.add, 111, ys); //=> 111
reject Added in v0.1.0
Filterable f => (a → Boolean) → f a → f a
Parameters
-
pred -
filterable
Returns
- Array
The complement of filter.
Acts as a transducer if a transformer is given in list position. Filterable objects include plain objects or any object that has a filter method such as Array.
See also filter, transduce, addIndex.
const isOdd = (n) => n % 2 !== 0;
R.reject(isOdd, [1, 2, 3, 4]); //=> [2, 4]
R.reject(isOdd, {a: 1, b: 2, c: 3, d: 4}); //=> {b: 2, d: 4} remove Added in v0.2.2
Number → Number → [a] → [a]
Parameters
-
startThe position to start removing elements
-
countThe number of elements to remove
-
listThe list to remove from
Returns
- Array A new Array with `count` elements from `start` removed.
Removes the sub-list of list starting at index start and containing count elements. Note that this is not destructive: it returns a copy of the list with the changes. No lists have been harmed in the application of this function.
See also without.
R.remove(2, 3, [1,2,3,4,5,6,7,8]); //=> [1,2,6,7,8]
renameKeys Added in v0.31.0
Object → Object → Object
Parameters
-
mappingAn object pairing existing keys with new ones
-
objA target object to convert
Returns
- Object The result of replacing existing keys with their mapping counterparts when such exist
Converts an object to a new one by changing all keys that are also found as keys in a mapping object to their corresponding values from that object.
var mapping = { name: 'firstName', address: 'street' };
var obj = { name: 'John', city: 'Paris' };
R.renameKeys(mapping, obj) //=> { firstName: 'John', city: 'Paris' } repeat Added in v0.1.1
a → n → [a]
Parameters
-
valueThe value to repeat.
-
nThe desired size of the output list.
Returns
- Array A new array containing `n` `value`s.
Returns a fixed list of size n containing a specified identical value.
See also times.
R.repeat('hi', 5); //=> ['hi', 'hi', 'hi', 'hi', 'hi']
const obj = {};
const repeatedObjs = R.repeat(obj, 5); //=> [{}, {}, {}, {}, {}]
repeatedObjs[0] === repeatedObjs[1]; //=> true replace Added in v0.7.0
RegExp|String → String → String → String
Parameters
-
patternA regular expression or a substring to match.
-
replacementThe string to replace the matches with.
-
strThe String to do the search and replacement in.
Returns
- String The result.
Replace a substring or regex match in a string with a replacement.
The first two parameters correspond to the parameters of the String.prototype.replace() function, so the second parameter can also be a function.
R.replace('foo', 'bar', 'foo foo foo'); //=> 'bar foo foo'
R.replace(/foo/, 'bar', 'foo foo foo'); //=> 'bar foo foo'
// Use the "g" (global) flag to replace all occurrences:
R.replace(/foo/g, 'bar', 'foo foo foo'); //=> 'bar bar bar' reverse Added in v0.1.0
[a] → [a]
String → StringParameters
-
list
Returns
- Array
Returns a new list or string with the elements or characters in reverse order.
R.reverse([1, 2, 3]); //=> [3, 2, 1]
R.reverse([1, 2]); //=> [2, 1]
R.reverse([1]); //=> [1]
R.reverse([]); //=> []
R.reverse('abc'); //=> 'cba'
R.reverse('ab'); //=> 'ba'
R.reverse('a'); //=> 'a'
R.reverse(''); //=> '' scan Added in v0.10.0
((a, b) → a) → a → [b] → [a]
Parameters
-
fnThe iterator function. Receives two values, the accumulator and the current element from the array
-
accThe accumulator value.
-
listThe list to iterate over.
Returns
- Array A list of all intermediately reduced values.
Scan is similar to reduce, but returns a list of successively reduced values from the left.
Acts as a transducer if a transformer is given in list position.
const numbers = [1, 2, 3, 4]; const factorials = R.scan(R.multiply, 1, numbers); //=> [1, 1, 2, 6, 24]
sequence Added in v0.19.0
fantasy-land/of :: TypeRep f => f ~> a → f a
(Applicative f, Traversable t) => TypeRep f → t (f a) → f (t a)(Applicative f, Traversable t) => (a → f a) → t (f a) → f (t a)Parameters
-
TypeRepresentativewith an
oforfantasy-land/ofmethod -
traversable
Returns
- *
Transforms a Traversable of Applicative into an Applicative of Traversable.
Dispatches to the "fantasy-land/traverse" or the traverse method of the second argument, if present.
See also traverse.
R.sequence(Maybe.of, [Just(1), Just(2), Just(3)]); //=> Just([1, 2, 3]) R.sequence(Maybe.of, [Just(1), Just(2), Nothing()]); //=> Nothing() R.sequence(R.of(Array), Just([1, 2, 3])); //=> [Just(1), Just(2), Just(3)] R.sequence(R.of(Array), Nothing()); //=> [Nothing()]
set Added in v0.16.0
Lens s a → a → s → s
Lens s a = Functor f => (a → f a) → s → f sParameters
-
lens -
v -
x
Returns
- *
Returns the result of "setting" the portion of the given data structure focused by the given lens to the given value.
See also view, over, lens, lensIndex, lensProp, lensPath.
const xLens = R.lensProp('x');
R.set(xLens, 4, {x: 1, y: 2}); //=> {x: 4, y: 2}
R.set(xLens, 8, {x: 1, y: 2}); //=> {x: 8, y: 2} slice Added in v0.1.4
Number → Number → [a] → [a]
Number → Number → String → StringParameters
-
fromIndexThe start index (inclusive).
-
toIndexThe end index (exclusive).
-
list
Returns
- *
Returns the elements of the given list or string (or object with a slice method) from fromIndex (inclusive) to toIndex (exclusive).
Dispatches to the slice method of the third argument, if present.
R.slice(1, 3, ['a', 'b', 'c', 'd']); //=> ['b', 'c'] R.slice(1, Infinity, ['a', 'b', 'c', 'd']); //=> ['b', 'c', 'd'] R.slice(0, -1, ['a', 'b', 'c', 'd']); //=> ['a', 'b', 'c'] R.slice(-3, -1, ['a', 'b', 'c', 'd']); //=> ['b', 'c'] R.slice(0, 3, 'ramda'); //=> 'ram'
sort Added in v0.1.0
((a, a) → Number) → [a] → [a]
Parameters
-
comparatorA sorting function :: a -> b -> Int
-
listThe list to sort
Returns
- Array a new array with its elements sorted by the comparator function.
Returns a copy of the list, sorted according to the comparator function, which should accept two values at a time and return a negative number if the first value is smaller, a positive number if it's larger, and zero if they are equal. Please note that this is a copy of the list. It does not modify the original.
const diff = function(a, b) { return a - b; };
R.sort(diff, [4,2,7,5]); //=> [2, 4, 5, 7] sortBy Added in v0.1.0
Ord b => (a → b) → [a] → [a]
Parameters
-
fn -
listThe list to sort.
Returns
- Array A new list sorted by the keys generated by `fn`.
Sorts the list according to the supplied function.
const sortByFirstItem = R.sortBy(R.prop(0));
const pairs = [[-1, 1], [-2, 2], [-3, 3]];
sortByFirstItem(pairs); //=> [[-3, 3], [-2, 2], [-1, 1]]
const sortByNameCaseInsensitive = R.sortBy(R.compose(R.toLower, R.prop('name')));
const alice = {
name: 'ALICE',
age: 101
};
const bob = {
name: 'Bob',
age: -10
};
const clara = {
name: 'clara',
age: 314.159
};
const people = [clara, bob, alice];
sortByNameCaseInsensitive(people); //=> [alice, bob, clara] sortWith Added in v0.23.0
[(a, a) → Number] → [a] → [a]
Parameters
-
functionsA list of comparator functions.
-
listThe list to sort.
Returns
- Array A new list sorted according to the comarator functions.
Sorts a list according to a list of comparators.
const alice = {
name: 'alice',
age: 40
};
const bob = {
name: 'bob',
age: 30
};
const clara = {
name: 'clara',
age: 40
};
const people = [clara, bob, alice];
const ageNameSort = R.sortWith([
R.descend(R.prop('age')),
R.ascend(R.prop('name'))
]);
ageNameSort(people); //=> [alice, clara, bob] split Added in v0.1.0
(String | RegExp) → String → [String]
Parameters
-
sepThe pattern.
-
strThe string to separate into an array.
Returns
- Array The array of strings from `str` separated by `sep`.
Splits a string into an array of strings based on the given separator.
See also join.
const pathComponents = R.split('/');
R.tail(pathComponents('/usr/local/bin/node')); //=> ['usr', 'local', 'bin', 'node']
R.split('.', 'a.b.c.xyz.d'); //=> ['a', 'b', 'c', 'xyz', 'd'] splitAt Added in v0.19.0
Number → [a] → [[a], [a]]
Number → String → [String, String]Parameters
-
indexThe index where the array/string is split.
-
arrayThe array/string to be split.
Returns
- Array
Splits a given list or string at a given index.
R.splitAt(1, [1, 2, 3]); //=> [[1], [2, 3]] R.splitAt(5, 'hello world'); //=> ['hello', ' world'] R.splitAt(-1, 'foobar'); //=> ['fooba', 'r']
splitEvery Added in v0.16.0
Number → [a] → [[a]]
Number → String → [String]Parameters
-
n -
list
Returns
- Array
Splits a collection into slices of the specified length.
R.splitEvery(3, [1, 2, 3, 4, 5, 6, 7]); //=> [[1, 2, 3], [4, 5, 6], [7]] R.splitEvery(3, 'foobarbaz'); //=> ['foo', 'bar', 'baz']
splitWhen Added in v0.19.0
(a → Boolean) → [a] → [[a], [a]]
Parameters
-
predThe predicate that determines where the array is split.
-
listThe array to be split.
Returns
- Array
Takes a list and a predicate and returns a pair of lists with the following properties:
- the result of concatenating the two output lists is equivalent to the input list;
- none of the elements of the first output list satisfies the predicate; and
- if the second output list is non-empty, its first element satisfies the predicate.
R.splitWhen(R.equals(2), [1, 2, 3, 1, 2, 3]); //=> [[1], [2, 3, 1, 2, 3]]
splitWhenever Added in v0.28.0
(a → Boolean) → [a] → [[a]]
Parameters
-
predThe predicate that determines where the array is split.
-
listThe array to be split.
Returns
- Array
Splits an array into slices on every occurrence of a value.
R.splitWhenever(R.equals(2), [1, 2, 3, 2, 4, 5, 2, 6, 7]); //=> [[1], [3], [4, 5], [6, 7]]
startsWith Added in v0.24.0
[a] → [a] → Boolean
String → String → BooleanParameters
-
prefix -
list
Returns
- Boolean
Checks if a list starts with the provided sublist.
Similarly, checks if a string starts with the provided substring.
See also endsWith.
R.startsWith('a', 'abc') //=> true
R.startsWith('b', 'abc') //=> false
R.startsWith(['a'], ['a', 'b', 'c']) //=> true
R.startsWith(['b'], ['a', 'b', 'c']) //=> false subtract Added in v0.1.0
Number → Number → Number
Parameters
-
aThe first value.
-
bThe second value.
Returns
- Number The result of `a - b`.
Subtracts its second argument from its first argument.
See also add.
R.subtract(10, 8); //=> 2 const minus5 = R.subtract(R.__, 5); minus5(17); //=> 12 const complementaryAngle = R.subtract(90); complementaryAngle(30); //=> 60 complementaryAngle(72); //=> 18
sum Added in v0.1.0
[Number] → Number
Parameters
-
listAn array of numbers
Returns
- Number The sum of all the numbers in the list.
Adds together all the elements of a list.
See also reduce.
R.sum([2,4,6,8,100,1]); //=> 121
swap Added in v0.29.0
Number → Number → [a] → [a]
Parameters
-
indexAThe first index
-
indexBThe second index
-
oEither the object or list which will serve to realise the swap
Returns
- Array The new object or list reordered
Swap an item, at index indexA with another item, at index indexB, in an object or a list of elements. A new result will be created containing the new elements order.
R.swap(0, 2, ['a', 'b', 'c', 'd', 'e', 'f']); //=> ['c', 'b', 'a', 'd', 'e', 'f']
R.swap(-1, 0, ['a', 'b', 'c', 'd', 'e', 'f']); //=> ['f', 'b', 'c', 'd', 'e', 'a']
R.swap('a', 'b', {a: 1, b: 2}); //=> {a: 2, b: 1}
R.swap(0, 2, 'foo'); //=> 'oof' symmetricDifference Added in v0.19.0
[*] → [*] → [*]
Parameters
-
list1The first list.
-
list2The second list.
Returns
- Array The elements in `list1` or `list2`, but not both.
Finds the set (i.e. no duplicates) of all elements contained in the first or second list, but not both.
See also symmetricDifferenceWith, difference, differenceWith.
R.symmetricDifference([1,2,3,4], [7,6,5,4,3]); //=> [1,2,7,6,5] R.symmetricDifference([7,6,5,4,3], [1,2,3,4]); //=> [7,6,5,1,2]
symmetricDifferenceWith Added in v0.19.0
((a, a) → Boolean) → [a] → [a] → [a]
Parameters
-
predA predicate used to test whether two items are equal.
-
list1The first list.
-
list2The second list.
Returns
- Array The elements in `list1` or `list2`, but not both.
Finds the set (i.e. no duplicates) of all elements contained in the first or second list, but not both. Duplication is determined according to the value returned by applying the supplied predicate to two list elements.
See also symmetricDifference, difference, differenceWith.
const eqA = R.eqBy(R.prop('a'));
const l1 = [{a: 1}, {a: 2}, {a: 3}, {a: 4}];
const l2 = [{a: 3}, {a: 4}, {a: 5}, {a: 6}];
R.symmetricDifferenceWith(eqA, l1, l2); //=> [{a: 1}, {a: 2}, {a: 5}, {a: 6}] T Added in v0.9.0
* → Boolean
Parameters
Returns
- Boolean
A function that always returns true. Any passed in parameters are ignored.
See also F.
R.T(); //=> true
tail Added in v0.1.0
[a] → [a]
String → StringParameters
-
list
Returns
- *
Returns all but the first element of the given list or string (or object with a tail method).
Dispatches to the slice method of the first argument, if present.
R.tail([1, 2, 3]); //=> [2, 3]
R.tail([1, 2]); //=> [2]
R.tail([1]); //=> []
R.tail([]); //=> []
R.tail('abc'); //=> 'bc'
R.tail('ab'); //=> 'b'
R.tail('a'); //=> ''
R.tail(''); //=> '' take Added in v0.1.0
Number → [a] → [a]
Number → String → StringParameters
-
n -
list
Returns
- *
Returns the first n elements of the given list, string, or transducer/transformer (or object with a take method).
Dispatches to the take method of the second argument, if present.
See also drop.
R.take(1, ['foo', 'bar', 'baz']); //=> ['foo'] R.take(2, ['foo', 'bar', 'baz']); //=> ['foo', 'bar'] R.take(3, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz'] R.take(4, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz'] R.take(3, 'ramda'); //=> 'ram' const personnel = [ 'Dave Brubeck', 'Paul Desmond', 'Eugene Wright', 'Joe Morello', 'Gerry Mulligan', 'Bob Bates', 'Joe Dodge', 'Ron Crotty' ]; const takeFive = R.take(5); takeFive(personnel); //=> ['Dave Brubeck', 'Paul Desmond', 'Eugene Wright', 'Joe Morello', 'Gerry Mulligan']
takeLast Added in v0.16.0
Number → [a] → [a]
Number → String → StringParameters
-
nThe number of elements to return.
-
xsThe collection to consider.
Returns
- Array
Returns a new list containing the last n elements of the given list. If n > list.length, returns a list of list.length elements.
See also dropLast.
R.takeLast(1, ['foo', 'bar', 'baz']); //=> ['baz'] R.takeLast(2, ['foo', 'bar', 'baz']); //=> ['bar', 'baz'] R.takeLast(3, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz'] R.takeLast(4, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz'] R.takeLast(3, 'ramda'); //=> 'mda'
takeLastWhile Added in v0.16.0
(a → Boolean) → [a] → [a]
(a → Boolean) → String → StringParameters
-
fnThe function called per iteration.
-
xsThe collection to iterate over.
Returns
- Array A new array.
Returns a new list containing the last n elements of a given list, passing each value to the supplied predicate function, and terminating when the predicate function returns false. Excludes the element that caused the predicate function to fail. The predicate function is passed one argument: (value).
See also dropLastWhile, addIndex.
const isNotOne = x => x !== 1; R.takeLastWhile(isNotOne, [1, 2, 3, 4]); //=> [2, 3, 4] R.takeLastWhile(x => x !== 'R' , 'Ramda'); //=> 'amda'
takeWhile Added in v0.1.0
(a → Boolean) → [a] → [a]
(a → Boolean) → String → StringParameters
-
fnThe function called per iteration.
-
xsThe collection to iterate over.
Returns
- Array A new array.
Returns a new list containing the first n elements of a given list, passing each value to the supplied predicate function, and terminating when the predicate function returns false. Excludes the element that caused the predicate function to fail. The predicate function is passed one argument: (value).
Dispatches to the takeWhile method of the second argument, if present.
Acts as a transducer if a transformer is given in list position.
See also dropWhile, transduce, addIndex.
const isNotFour = x => x !== 4; R.takeWhile(isNotFour, [1, 2, 3, 4, 3, 2, 1]); //=> [1, 2, 3] R.takeWhile(x => x !== 'd' , 'Ramda'); //=> 'Ram'
tap Added in v0.1.0
(a → *) → a → a
Parameters
-
fnThe function to call with
x. The return value offnwill be thrown away. -
x
Returns
- * `x`.
Runs the given function with the supplied object, then returns the object.
Acts as a transducer if a transformer is given as second parameter.
const sayX = x => console.log('x is ' + x);
R.tap(sayX, 100); //=> 100
// logs 'x is 100' test Added in v0.12.0
RegExp → String → Boolean
Parameters
-
pattern -
str
Returns
- Boolean
Determines whether a given string matches a given regular expression.
See also match.
R.test(/^x/, 'xyz'); //=> true R.test(/^y/, 'xyz'); //=> false
thunkify Added in v0.26.0
((a, b, …, j) → k) → (a, b, …, j) → (() → k)
Parameters
-
fnA function to wrap in a thunk
Returns
- function Expects arguments for `fn` and returns a new function that, when called, applies those arguments to `fn`.
Creates a thunk out of a function. A thunk delays a calculation until its result is needed, providing lazy evaluation of arguments.
See also partial, partialRight.
R.thunkify(R.identity)(42)(); //=> 42 R.thunkify((a, b) => a + b)(25, 17)(); //=> 42
times Added in v0.2.3
(Number → a) → Number → [a]
Parameters
-
fnThe function to invoke. Passed one argument, the current value of
n. -
nA value between
0andn - 1. Increments after each function call.
Returns
- Array An array containing the return values of all calls to `fn`.
Calls an input function n times, returning an array containing the results of those function calls.
fn is passed one argument: The current value of n, which begins at 0 and is gradually incremented to n - 1.
See also repeat.
R.times(R.identity, 5); //=> [0, 1, 2, 3, 4]
toLower Added in v0.9.0
String → String
Parameters
-
strThe string to lower case.
Returns
- String The lower case version of `str`.
The lower case version of a string.
See also toUpper.
R.toLower('XYZ'); //=> 'xyz' toPairs Added in v0.4.0
{String: *} → [[String,*]] Parameters
-
objThe object to extract from
Returns
- Array An array of key, value arrays from the object's own properties.
Converts an object into an array of key, value arrays. Only the object's own properties are used. Note that the order of the output array is not guaranteed to be consistent across different JS platforms.
See also fromPairs, keys, values.
R.toPairs({a: 1, b: 2, c: 3}); //=> [['a', 1], ['b', 2], ['c', 3]] toPairsIn Added in v0.4.0
{String: *} → [[String,*]] Parameters
-
objThe object to extract from
Returns
- Array An array of key, value arrays from the object's own and prototype properties.
Converts an object into an array of key, value arrays. The object's own properties and prototype properties are used. Note that the order of the output array is not guaranteed to be consistent across different JS platforms.
const F = function() { this.x = 'X'; };
F.prototype.y = 'Y';
const f = new F();
R.toPairsIn(f); //=> [['x','X'], ['y','Y']] toString Added in v0.14.0
* → String
Parameters
-
val
Returns
- String
Returns the string representation of the given value. eval'ing the output should result in a value equivalent to the input value. Many of the built-in toString methods do not satisfy this requirement.
If the given value is an [object Object] with a toString method other than Object.prototype.toString, this method is invoked with no arguments to produce the return value. This means user-defined constructor functions can provide a suitable toString method. For example:
function Point(x, y) {
this.x = x;
this.y = y;
}
Point.prototype.toString = function() {
return 'new Point(' + this.x + ', ' + this.y + ')';
};
R.toString(new Point(1, 2)); //=> 'new Point(1, 2)'
R.toString(42); //=> '42'
R.toString('abc'); //=> '"abc"'
R.toString([1, 2, 3]); //=> '[1, 2, 3]'
R.toString({foo: 1, bar: 2, baz: 3}); //=> '{"bar": 2, "baz": 3, "foo": 1}'
R.toString(new Date('2001-02-03T04:05:06Z')); //=> 'new Date("2001-02-03T04:05:06.000Z")' toUpper Added in v0.9.0
String → String
Parameters
-
strThe string to upper case.
Returns
- String The upper case version of `str`.
The upper case version of a string.
See also toLower.
R.toUpper('abc'); //=> 'ABC' transduce Added in v0.12.0
(c → c) → ((a, b) → a) → a → [b] → a
Parameters
-
xfThe transducer function. Receives a transformer and returns a transformer.
-
fnThe 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
-
accThe initial accumulator value.
-
listThe list to iterate over.
Returns
- * The final, accumulated value.
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 R.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 R.reduce after initializing the transducer.
See also reduce, reduced, into.
const numbers = [1, 2, 3, 4]; const transducer = R.compose(R.map(R.add(1)), R.take(2)); R.transduce(transducer, R.flip(R.append), [], numbers); //=> [2, 3] const isOdd = (x) => x % 2 !== 0; const firstOddTransducer = R.compose(R.filter(isOdd), R.take(1)); R.transduce(firstOddTransducer, R.flip(R.append), [], R.range(0, 100)); //=> [1]
transpose Added in v0.19.0
[[a]] → [[a]]
Parameters
-
listA 2D list
Returns
- Array A 2D list
Transposes the rows and columns of a 2D list. When passed a list of n lists of length x, returns a list of x lists of length n.
R.transpose([[1, 'a'], [2, 'b'], [3, 'c']]) //=> [[1, 2, 3], ['a', 'b', 'c']] R.transpose([[1, 2, 3], ['a', 'b', 'c']]) //=> [[1, 'a'], [2, 'b'], [3, 'c']] // If some of the rows are shorter than the following rows, their elements are skipped: R.transpose([[10, 11], [20], [], [30, 31, 32]]) //=> [[10, 20, 30], [11, 31], [32]]
traverse Added in v0.19.0
fantasy-land/of :: TypeRep f => f ~> a → f a
(Applicative f, Traversable t) => TypeRep f → (a → f b) → t a → f (t b)(Applicative f, Traversable t) => (b → f b) → (a → f b) → t a → f (t b)Parameters
-
TypeRepresentativewith an
oforfantasy-land/ofmethod -
f -
traversable
Returns
- *
Maps an Applicative-returning function over a Traversable, then uses sequence to transform the resulting Traversable of Applicative into an Applicative of Traversable.
Dispatches to the traverse method of the third argument, if present.
See also sequence.
// Returns `Maybe.Nothing` if the given divisor is `0`
const safeDiv = n => d => d === 0 ? Maybe.Nothing() : Maybe.Just(n / d)
R.traverse(Maybe.of, safeDiv(10), [2, 4, 5]); //=> Maybe.Just([5, 2.5, 2])
R.traverse(Maybe.of, safeDiv(10), [2, 0, 5]); //=> Maybe.Nothing
// Using a Type Representative
R.traverse(Maybe, safeDiv(10), Right(4)); //=> Just(Right(2.5))
R.traverse(Maybe, safeDiv(10), Right(0)); //=> Nothing
R.traverse(Maybe, safeDiv(10), Left("X")); //=> Just(Left("X")) trim Added in v0.6.0
String → String
Parameters
-
strThe string to trim.
Returns
- String Trimmed version of `str`.
Removes (strips) whitespace from both ends of the string.
R.trim(' xyz '); //=> 'xyz'
R.map(R.trim, R.split(',', 'x, y, z')); //=> ['x', 'y', 'z'] tryCatch Added in v0.20.0
(…x → a) → ((e, …x) → a) → (…x → a)
Parameters
-
tryerThe function that may throw.
-
catcherThe function that will be evaluated if
tryerthrows.
Returns
- function A new function that will catch exceptions and send them to the catcher.
tryCatch takes two functions, a tryer and a catcher. The returned function evaluates the tryer; if it does not throw, it simply returns the result. If the tryer does throw, the returned function evaluates the catcher function and returns its result. Note that for effective composition with this function, both the tryer and catcher functions must return the same type of results.
R.tryCatch(R.prop('x'), R.F)({x: true}); //=> true
R.tryCatch(() => { throw 'foo'}, R.always('caught'))('bar') // =>
'caught'
R.tryCatch(R.times(R.identity), R.always([]))('s') // => []
R.tryCatch(() => { throw 'this is not a valid value'}, (err, value)=>({error : err, value }))('bar') // => {'error': 'this is not a valid value', 'value': 'bar'} type Added in v0.8.0
* → String
Parameters
-
valThe value to test
Returns
- String
Gives a single-word string description of the (native) type of a value, returning such answers as 'Object', 'Number', 'Array', or 'Null'. Does not attempt to distinguish user Object types any further, reporting them all as 'Object'.
R.type({}); //=> "Object"
R.type(1); //=> "Number"
R.type(false); //=> "Boolean"
R.type('s'); //=> "String"
R.type(null); //=> "Null"
R.type([]); //=> "Array"
R.type(/[A-z]/); //=> "RegExp"
R.type(() => {}); //=> "Function"
R.type(async () => {}); //=> "AsyncFunction"
R.type(undefined); //=> "Undefined"
R.type(BigInt(123)); //=> "BigInt" unapply Added in v0.8.0
([*…] → a) → (*… → a)
Parameters
-
fn
Returns
- function
Takes a function fn, which takes a single array argument, and returns a function which:
- takes any number of positional arguments;
- passes these arguments to
fnas an array; and - returns the result.
In other words, R.unapply derives a variadic function from a function which takes an array. R.unapply is the inverse of R.apply.
See also apply.
R.unapply(JSON.stringify)(1, 2, 3); //=> '[1,2,3]'
unary Added in v0.2.0
(a → b → c → … → z) → (a → z)
Parameters
-
fnThe function to wrap.
Returns
- function A new function wrapping `fn`. The new function is guaranteed to be of arity 1.
Wraps a function of any arity (including nullary) in a function that accepts exactly 1 parameter. Any extraneous parameters will not be passed to the supplied function.
const takesTwoArgs = function(a, b) {
return [a, b];
};
takesTwoArgs.length; //=> 2
takesTwoArgs(1, 2); //=> [1, 2]
const takesOneArg = R.unary(takesTwoArgs);
takesOneArg.length; //=> 1
// Only 1 argument is passed to the wrapped function
takesOneArg(1, 2); //=> [1, undefined] uncurryN Added in v0.14.0
Number → (a → b → c … → z) → ((a → b → c …) → z)
Parameters
-
lengthThe arity for the returned function.
-
fnThe function to uncurry.
Returns
- function A new function.
Returns a function of arity n from a (manually) curried function. Note that, the returned function is actually a ramda style curryied function, which can accept one or more arguments in each function calling.
const addFour = a => b => c => d => a + b + c + d; const uncurriedAddFour = R.uncurryN(4, addFour); uncurriedAddFour(1, 2, 3, 4); //=> 10
unfold Added in v0.10.0
(a → [b]) → * → [b]
Parameters
-
fnThe iterator function. receives one argument,
seed, and returns either false to quit iteration or an array of length two to proceed. The element at index 0 of this array will be added to the resulting array, and the element at index 1 will be passed to the next call tofn. -
seedThe seed value.
Returns
- Array The final list.
Builds a list from a seed value. Accepts an iterator function, which returns either false to stop iteration or an array of length 2 containing the value to add to the resulting list and the seed to be used in the next call to the iterator function.
The iterator function receives one argument: (seed).
const f = n => n > 50 ? false : [-n, n + 10]; R.unfold(f, 10); //=> [-10, -20, -30, -40, -50]
union Added in v0.1.0
[*] → [*] → [*]
Parameters
-
asThe first list.
-
bsThe second list.
Returns
- Array The first and second lists concatenated, with duplicates removed.
Combines two lists into a set (i.e. no duplicates) composed of the elements of each list.
R.union([1, 2, 3], [2, 3, 4]); //=> [1, 2, 3, 4]
unionWith Added in v0.1.0
((a, a) → Boolean) → [*] → [*] → [*]
Parameters
-
predA predicate used to test whether two items are equal.
-
list1The first list.
-
list2The second list.
Returns
- Array The first and second lists concatenated, with duplicates removed.
Combines two lists into a set (i.e. no duplicates) composed of the elements of each list. Duplication is determined according to the value returned by applying the supplied predicate to two list elements. If an element exists in both lists, the first element from the first list will be used.
See also union.
const l1 = [{a: 1}, {a: 2}];
const l2 = [{a: 1}, {a: 4}];
R.unionWith(R.eqBy(R.prop('a')), l1, l2); //=> [{a: 1}, {a: 2}, {a: 4}] uniq Added in v0.1.0
[a] → [a]
Parameters
-
listThe array to consider.
Returns
- Array The list of unique items.
Returns a new list containing only one copy of each element in the original list. R.equals is used to determine equality.
R.uniq([1, 1, 2, 1]); //=> [1, 2] R.uniq([1, '1']); //=> [1, '1'] R.uniq([[42], [42]]); //=> [[42]]
uniqBy Added in v0.16.0
(a → b) → [a] → [a]
Parameters
-
fnA function used to produce a value to use during comparisons.
-
listThe array to consider.
Returns
- Array The list of unique items.
Returns a new list containing only one copy of each element in the original list, based upon the value returned by applying the supplied function to each list element. Prefers the first item if the supplied function produces the same value on two items. R.equals is used for comparison.
Acts as a transducer if a transformer is given in list position.
R.uniqBy(Math.abs, [-1, -5, 2, 10, 1, 2]); //=> [-1, -5, 2, 10]
uniqWith Added in v0.2.0
((a, a) → Boolean) → [a] → [a]
Parameters
-
predA predicate used to test whether two items are equal.
-
listThe array to consider.
Returns
- Array The list of unique items.
Returns a new list containing only one copy of each element in the original list, based upon the value returned by applying the supplied predicate to two list elements. Prefers the first item if two items compare equal based on the predicate.
Acts as a transducer if a transformer is given in list position.
const strEq = R.eqBy(String);
R.uniqWith(strEq)([1, '1', 2, 1]); //=> [1, 2]
R.uniqWith(strEq)([{}, {}]); //=> [{}]
R.uniqWith(strEq)([1, '1', 1]); //=> [1]
R.uniqWith(strEq)(['1', 1, 1]); //=> ['1'] unless Added in v0.18.0
(a → Boolean) → (a → b) → a → a | b
Parameters
-
predA predicate function
-
whenFalseFnA function to invoke when the
predevaluates to a falsy value. -
xAn object to test with the
predfunction and pass towhenFalseFnif necessary.
Returns
- * Either `x` or the result of applying `x` to `whenFalseFn`.
Tests the final argument by passing it to the given predicate function. If the predicate is not satisfied, the function will return the result of calling the whenFalseFn function with the same argument. If the predicate is satisfied, the argument is returned as is.
let safeInc = R.unless(R.isNil, R.inc); safeInc(null); //=> null safeInc(1); //=> 2
unnest Added in v0.3.0
Chain c => c (c a) → c a
Parameters
-
list
Returns
- *
Shorthand for R.chain(R.identity), which removes one level of nesting from any Chain.
R.unnest([1, [2], [[3]]]); //=> [1, 2, [3]] R.unnest([[1, 2], [3, 4], [5, 6]]); //=> [1, 2, 3, 4, 5, 6]
until Added in v0.20.0
(a → Boolean) → (a → a) → a → a
Parameters
-
predA predicate function
-
fnThe iterator function
-
initInitial value
Returns
- * Final value that satisfies predicate
Takes a predicate, a transformation function, and an initial value, and returns a value of the same type as the initial value. It does so by applying the transformation until the predicate is satisfied, at which point it returns the satisfactory value.
R.until(R.gt(R.__, 100), R.multiply(2))(1) // => 128
unwind Added in v0.28.0
String → {k: [v]} → [{k: v}] Parameters
-
keyThe key to determine which property of the object should be unwound.
-
objectThe object containing the list to unwind at the property named by the key.
Returns
- List A list of new objects, each having the given key associated to an item from the unwound list.
Deconstructs an array field from the input documents to output a document for each element. Each output document is the input document with the value of the array field replaced by the element.
R.unwind('hobbies', {
name: 'alice',
hobbies: ['Golf', 'Hacking'],
colors: ['red', 'green'],
});
// [
// { name: 'alice', hobbies: 'Golf', colors: ['red', 'green'] },
// { name: 'alice', hobbies: 'Hacking', colors: ['red', 'green'] }
// ] update Added in v0.14.0
Number → a → [a] → [a]
Parameters
-
idxThe index to update.
-
xThe value to exist at the given index of the returned array.
-
listThe source array-like object to be updated.
Returns
- Array A copy of `list` with the value at index `idx` replaced with `x`.
Returns a new copy of the array with the element at the provided index replaced with the given value.
When idx < -list.length || idx >= list.length, the original list is returned.
See also adjust.
R.update(1, '_', ['a', 'b', 'c']); //=> ['a', '_', 'c'] R.update(-1, '_', ['a', 'b', 'c']); //=> ['a', 'b', '_'] // out-of-range returns original list R.update(3, '_', ['a', 'b', 'c']); //=> ['a', 'b', 'c'] R.update(-4, '_', ['a', 'b', 'c']); //=> ['a', 'b', 'c']
useWith Added in v0.1.0
((x1, x2, …) → z) → [(a → x1), (b → x2), …] → (a → b → … → z)
Parameters
-
fnThe function to wrap.
-
transformersA list of transformer functions
Returns
- function The wrapped function.
Accepts a function fn and a list of transformer functions and returns a new curried function. When the new function is invoked, it calls the function fn with parameters consisting of the result of calling each supplied handler on successive arguments to the new function.
If more arguments are passed to the returned function than transformer functions, those arguments are passed directly to fn as additional parameters. If you expect additional arguments that don't need to be transformed, although you can ignore them, it's best to pass an identity function so that the new function reports the correct arity.
See also converge.
R.useWith(Math.pow, [R.identity, R.identity])(3, 4); //=> 81 R.useWith(Math.pow, [R.identity, R.identity])(3)(4); //=> 81 R.useWith(Math.pow, [R.dec, R.inc])(3, 4); //=> 32 R.useWith(Math.pow, [R.dec, R.inc])(3)(4); //=> 32
values Added in v0.1.0
{k: v} → [v] Parameters
-
objThe object to extract values from
Returns
- Array An array of the values of the object's own properties.
Returns a list of all the enumerable own properties of the supplied object. Note that the order of the output array is not guaranteed across different JS platforms.
See also valuesIn, keys, toPairs.
R.values({a: 1, b: 2, c: 3}); //=> [1, 2, 3] valuesIn Added in v0.2.0
{k: v} → [v] Parameters
-
objThe object to extract values from
Returns
- Array An array of the values of the object's own and prototype properties.
Returns a list of all the properties, including prototype properties, of the supplied object. Note that the order of the output array is not guaranteed to be consistent across different JS platforms.
const F = function() { this.x = 'X'; };
F.prototype.y = 'Y';
const f = new F();
R.valuesIn(f); //=> ['X', 'Y'] view Added in v0.16.0
Lens s a → s → a
Lens s a = Functor f => (a → f a) → s → f sParameters
-
lens -
x
Returns
- *
Returns a "view" of the given data structure, determined by the given lens. The lens's focus determines which portion of the data structure is visible.
See also set, over, lens, lensIndex, lensProp, lensPath.
const xLens = R.lensProp('x');
R.view(xLens, {x: 1, y: 2}); //=> 1
R.view(xLens, {x: 4, y: 2}); //=> 4 when Added in v0.18.0
(a → Boolean) → (a → b) → a → a | b
Parameters
-
predA predicate function
-
whenTrueFnA function to invoke when the
conditionevaluates to a truthy value. -
xAn object to test with the
predfunction and pass towhenTrueFnif necessary.
Returns
- * Either `x` or the result of applying `x` to `whenTrueFn`.
Tests the final argument by passing it to the given predicate function. If the predicate is satisfied, the function will return the result of calling the whenTrueFn function with the same argument. If the predicate is not satisfied, the argument is returned as is.
See also ifElse, unless, cond.
// truncate :: String -> String
const truncate = R.when(
R.propSatisfies(R.gt(R.__, 10), 'length'),
R.pipe(R.take(10), R.append('…'), R.join(''))
);
truncate('12345'); //=> '12345'
truncate('0123456789ABC'); //=> '0123456789…' where Added in v0.1.1
{String: (* → Boolean)} → {String: *} → Boolean Parameters
-
spec -
testObj
Returns
- Boolean
Takes a spec object and a test object; returns true if the test satisfies the spec. Each of the spec's own properties must be a predicate function. Each predicate is applied to the value of the corresponding property of the test object. where returns true if all the predicates return true, false otherwise.
where is well suited to declaratively expressing constraints for other functions such as filter and find.
See also propSatisfies, whereEq.
// pred :: Object -> Boolean
const pred = R.where({
a: R.equals('foo'),
b: R.complement(R.equals('bar')),
x: R.gt(R.__, 10),
y: R.lt(R.__, 20)
});
pred({a: 'foo', b: 'xxx', x: 11, y: 19}); //=> true
pred({a: 'xxx', b: 'xxx', x: 11, y: 19}); //=> false
pred({a: 'foo', b: 'bar', x: 11, y: 19}); //=> false
pred({a: 'foo', b: 'xxx', x: 10, y: 19}); //=> false
pred({a: 'foo', b: 'xxx', x: 11, y: 20}); //=> false whereAny Added in v0.28.0
{String: (* → Boolean)} → {String: *} → Boolean Parameters
-
spec -
testObj
Returns
- Boolean
Takes a spec object and a test object; each of the spec's own properties must be a predicate function. Each predicate is applied to the value of the corresponding property of the test object. whereAny returns true if at least one of the predicates return true, false otherwise.
whereAny is well suited to declaratively expressing constraints for other functions such as filter and find.
See also propSatisfies, where.
// pred :: Object -> Boolean
const pred = R.whereAny({
a: R.equals('foo'),
b: R.complement(R.equals('xxx')),
x: R.gt(R.__, 10),
y: R.lt(R.__, 20)
});
pred({a: 'foo', b: 'xxx', x: 8, y: 34}); //=> true
pred({a: 'xxx', b: 'xxx', x: 9, y: 21}); //=> false
pred({a: 'bar', b: 'xxx', x: 10, y: 20}); //=> false
pred({a: 'foo', b: 'bar', x: 10, y: 20}); //=> true
pred({a: 'foo', b: 'xxx', x: 11, y: 20}); //=> true whereEq Added in v0.14.0
{String: *} → {String: *} → Boolean Parameters
-
spec -
testObj
Returns
- Boolean
Takes a spec object and a test object; returns true if the test satisfies the spec, false otherwise. An object satisfies the spec if, for each of the spec's own properties, accessing that property of the object gives the same value (in R.equals terms) as accessing that property of the spec.
whereEq is a specialization of where.
// pred :: Object -> Boolean
const pred = R.whereEq({a: 1, b: 2});
pred({a: 1}); //=> false
pred({a: 1, b: 2}); //=> true
pred({a: 1, b: 2, c: 3}); //=> true
pred({a: 1, b: 1}); //=> false without Added in v0.19.0
[a] → [a] → [a]
Parameters
-
list1The values to be removed from
list2. -
list2The array to remove values from.
Returns
- Array The new array without values in `list1`.
Returns a new list without values in the first argument. R.equals is used to determine equality.
Acts as a transducer if a transformer is given in list position.
See also transduce, difference, remove.
R.without([1, 2], [1, 2, 1, 3, 4]); //=> [3, 4]
xor Added in v0.27.1
a → b → Boolean
Parameters
-
a -
b
Returns
- Boolean true if one of the arguments is truthy and the other is falsy
Exclusive disjunction logical operation. Returns true if one of the arguments is truthy and the other is falsy. Otherwise, it returns false.
R.xor(true, true); //=> false R.xor(true, false); //=> true R.xor(false, true); //=> true R.xor(false, false); //=> false
xprod Added in v0.1.0
[a] → [b] → [[a,b]]
Parameters
-
asThe first list.
-
bsThe second list.
Returns
- Array The list made by combining each possible pair from `as` and `bs` into pairs (`[a, b]`).
Creates a new list out of the two supplied by creating each possible pair from the lists.
R.xprod([1, 2], ['a', 'b']); //=> [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]
zip Added in v0.1.0
[a] → [b] → [[a,b]]
Parameters
-
list1The first array to consider.
-
list2The second array to consider.
Returns
- Array The list made by pairing up same-indexed elements of `list1` and `list2`.
Creates a new list out of the two supplied by pairing up equally-positioned items from both lists. The returned list is truncated to the length of the shorter of the two input lists. Note: zip is equivalent to zipWith(function(a, b) { return [a, b] }).
R.zip([1, 2, 3], ['a', 'b', 'c']); //=> [[1, 'a'], [2, 'b'], [3, 'c']]
zipObj Added in v0.3.0
[String] → [*] → {String: *} Parameters
-
keysThe array that will be properties on the output object.
-
valuesThe list of values on the output object.
Returns
- Object The object made by pairing up same-indexed elements of `keys` and `values`.
Creates a new object out of a list of keys and a list of values. Key/value pairing is truncated to the length of the shorter of the two lists. Note: zipObj is equivalent to pipe(zip, fromPairs).
R.zipObj(['a', 'b', 'c'], [1, 2, 3]); //=> {a: 1, b: 2, c: 3} zipWith Added in v0.1.0
((a, b) → c) → [a] → [b] → [c]
Parameters
-
fnThe function used to combine the two elements into one value.
-
list1The first array to consider.
-
list2The second array to consider.
Returns
- Array The list made by combining same-indexed elements of `list1` and `list2` using `fn`.
Creates a new list out of the two supplied by applying the function to each equally-positioned pair in the lists. The returned list is truncated to the length of the shorter of the two input lists.
const f = (x, y) => {
// ...
};
R.zipWith(f, [1, 2, 3], ['a', 'b', 'c']);
//=> [f(1, 'a'), f(2, 'b'), f(3, 'c')]
© 2013–2025 Scott Sauyet and Michael Hurley
Licensed under the MIT License.
https://ramdajs.com/0.31.3/docs/