all


(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.

Added in v0.1.0

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.

  1. const equals3 = R.equals(3);
  2. R.all(equals3)([3, 3, 3, 3]); //=> true
  3. R.all(equals3)([3, 3, 1, 3]); //=> false

any


(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.

Added in v0.1.0

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.

  1. const lessThan0 = R.flip(R.lt)(0);
  2. const lessThan2 = R.flip(R.lt)(2);
  3. R.any(lessThan0)([1, 2]); //=> false
  4. R.any(lessThan2)([1, 2]); //=> true

append


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.

Added in v0.1.0

Returns a new list containing the contents of the given list, followed by the given element.

See also prepend.

  1. R.append('tests', ['write', 'more']); //=> ['write', 'more', 'tests']
  2. R.append('tests', []); //=> ['tests']
  3. R.append(['tests'], ['write', 'more']); //=> ['write', 'more', ['tests']]

concat


[a] → [a] → [a]

String → String → String

Parameters

  • firstListThe first list
  • secondListThe second list

Returns Array A list consisting of the elements of firstList followed by the elements of secondList.

Added in v0.1.0

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.

  1. R.concat('ABC', 'DEF'); // 'ABCDEF'
  2. R.concat([4, 5, 6], [1, 2, 3]); //=> [4, 5, 6, 1, 2, 3]
  3. R.concat([], []); //=> []

drop


Number → [a] → [a]

Number → String → String

Parameters

  • n
  • list

Returns * A copy of list without the first n elements

Added in v0.1.0

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.

  1. R.drop(1, ['foo', 'bar', 'baz']); //=> ['bar', 'baz']
  2. R.drop(2, ['foo', 'bar', 'baz']); //=> ['baz']
  3. R.drop(3, ['foo', 'bar', 'baz']); //=> []
  4. R.drop(4, ['foo', 'bar', 'baz']); //=> []
  5. R.drop(3, 'ramda'); //=> 'da'

zipWith


((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.

Added in v0.1.0

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.

  1. const f = (x, y) => {
  2. // ...
  3. };
  4. R.zipWith(f, [1, 2, 3], ['a', 'b', 'c']);
  5. //=> [f(1, 'a'), f(2, 'b'), f(3, 'c')]

zip


[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.

Added in v0.1.0

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] }).

  1. R.zip([1, 2, 3], ['a', 'b', 'c']); //=> [[1, 'a'], [2, 'b'], [3, 'c']]

xprod


[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]).

Added in v0.1.0

Creates a new list out of the two supplied by creating each possible pair from the lists.

  1. R.xprod([1, 2], ['a', 'b']); //=> [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]

uniq


[a] → [a]

Parameters

  • listThe array to consider.

Returns Array The list of unique items.

Added in v0.1.0

Returns a new list containing only one copy of each element in the original list. R.equals is used to determine equality.

  1. R.uniq([1, 1, 2, 1]); //=> [1, 2]
  2. R.uniq([1, '1']); //=> [1, '1']
  3. R.uniq([[42], [42]]); //=> [[42]]

filter


Filterable f => (a → Boolean) → f a → f a

Parameters

  • pred
  • filterable

Returns Array Filterable

Added in v0.1.0

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 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.

  1. const isEven = n => n % 2 === 0;
  2. R.filter(isEven, [1, 2, 3, 4]); //=> [2, 4]
  3. R.filter(isEven, {a: 1, b: 2, c: 3, d: 4}); //=> {b: 2, d: 4}

find


(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.

Added in v0.1.0

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.

  1. const xs = [{a: 1}, {a: 2}, {a: 3}];
  2. R.find(R.propEq(2, 'a'))(xs); //=> {a: 2}
  3. R.find(R.propEq(4, 'a'))(xs); //=> undefined

flatten


[a] → [b]

Parameters

  • listThe array to consider.

Returns Array The flattened list.

Added in v0.1.0

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.

  1. R.flatten([1, 2, [3, 4], 5, [6, [7, 8, [9, [10, 11], 12]]]]);
  2. //=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

head


[a] → a | Undefined

String → String | Undefined

Added in v0.1.0

Returns the first element of the given list or string. In some libraries this function is named first.

See also tail, init, last.

  1. R.head(['fi', 'fo', 'fum']); //=> 'fi'
  2. R.head([]); //=> undefined
  3. R.head('abc'); //=> 'a'
  4. R.head(''); //=> undefined

indexOf


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.

Added in v0.1.0

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.

  1. R.indexOf(3, [1,2,3,4]); //=> 2
  2. R.indexOf(10, [1,2,3,4]); //=> -1

join


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.

Added in v0.1.0

Returns a string made by inserting the separator between each element and concatenating all the elements into a single string.

See also split.

  1. const spacer = R.join(' ');
  2. spacer(['a', 2, 3.4]); //=> 'a 2 3.4'
  3. R.join('|', [1, 2, 3]); //=> '1|2|3'

lastIndexOf


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.

Added in v0.1.0

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.

  1. R.lastIndexOf(3, [-1,3,3,0,1,2,3,4]); //=> 6
  2. R.lastIndexOf(10, [1,2,3,4]); //=> -1

map


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.

Added in v0.1.0

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.

  1. const double = x => x * 2;
  2. R.map(double, [1, 2, 3]); //=> [2, 4, 6]
  3. R.map(double, {x: 1, y: 2, z: 3}); //=> {x: 2, y: 4, z: 6}

nth


Number → [a] → a | Undefined

Number → String → String | Undefined

Added in v0.1.0

Returns the nth element of the given list or string. If n is negative the element at index length + n is returned.

  1. const list = ['foo', 'bar', 'baz', 'quux'];
  2. R.nth(1, list); //=> 'bar'
  3. R.nth(-1, list); //=> 'quux'
  4. R.nth(-99, list); //=> undefined
  5. R.nth(2, 'abc'); //=> 'c'
  6. R.nth(3, 'abc'); //=> undefined

pluck


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.

Added in v0.1.0

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.

  1. var getAges = R.pluck('age');
  2. getAges([{name: 'fred', age: 29}, {name: 'wilma', age: 27}]); //=> [29, 27]
  3. R.pluck(0, [[1, 2], [3, 4]]); //=> [1, 3]
  4. R.pluck('val', {a: {val: 3}, b: {val: 5}}); //=> {a: 3, b: 5}

prepend


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.

Added in v0.1.0

Returns a new list with the given element at the front, followed by the contents of the list.

See also append.

  1. R.prepend('fee', ['fi', 'fo', 'fum']); //=> ['fee', 'fi', 'fo', 'fum']

range


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).

Added in v0.1.0

Returns a list of numbers from from (inclusive) to to (exclusive).

  1. R.range(1, 5); //=> [1, 2, 3, 4]
  2. R.range(50, 53); //=> [50, 51, 52]

reduce


((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.

Added in v0.1.0

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.

  1. R.reduce(R.subtract, 0, [1, 2, 3, 4]) // => ((((0 - 1) - 2) - 3) - 4) = -10
  2. // - -10
  3. // / \ / \
  4. // - 4 -6 4
  5. // / \ / \
  6. // - 3 ==> -3 3
  7. // / \ / \
  8. // - 2 -1 2
  9. // / \ / \
  10. // 0 1 0 1

reduceRight


((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.

Added in v0.1.0

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.

  1. R.reduceRight(R.subtract, 0, [1, 2, 3, 4]) // => (1 - (2 - (3 - (4 - 0)))) = -2
  2. // - -2
  3. // / \ / \
  4. // 1 - 1 3
  5. // / \ / \
  6. // 2 - ==> 2 -1
  7. // / \ / \
  8. // 3 - 3 4
  9. // / \ / \
  10. // 4 0 4 0

reject


Filterable f => (a → Boolean) → f a → f a

Parameters

  • pred
  • filterable

Returns Array

Added in v0.1.0

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.

  1. const isOdd = (n) => n % 2 !== 0;
  2. R.reject(isOdd, [1, 2, 3, 4]); //=> [2, 4]
  3. R.reject(isOdd, {a: 1, b: 2, c: 3, d: 4}); //=> {b: 2, d: 4}

reverse


[a] → [a]

String → String

Added in v0.1.0

Returns a new list or string with the elements or characters in reverse order.

  1. R.reverse([1, 2, 3]); //=> [3, 2, 1]
  2. R.reverse([1, 2]); //=> [2, 1]
  3. R.reverse([1]); //=> [1]
  4. R.reverse([]); //=> []
  5. R.reverse('abc'); //=> 'cba'
  6. R.reverse('ab'); //=> 'ba'
  7. R.reverse('a'); //=> 'a'
  8. R.reverse(''); //=> ''

sort


((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.

Added in v0.1.0

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.

See also ascend, descend.

  1. const diff = function(a, b) { return a - b; };
  2. R.sort(diff, [4,2,7,5]); //=> [2, 4, 5, 7]

tail


[a] → [a]

String → String

Added in v0.1.0

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.

See also head, init, last.

  1. R.tail([1, 2, 3]); //=> [2, 3]
  2. R.tail([1, 2]); //=> [2]
  3. R.tail([1]); //=> []
  4. R.tail([]); //=> []
  5. R.tail('abc'); //=> 'bc'
  6. R.tail('ab'); //=> 'b'
  7. R.tail('a'); //=> ''
  8. R.tail(''); //=> ''

take


Number → [a] → [a]

Number → String → String

Added in v0.1.0

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.

  1. R.take(1, ['foo', 'bar', 'baz']); //=> ['foo']
  2. R.take(2, ['foo', 'bar', 'baz']); //=> ['foo', 'bar']
  3. R.take(3, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz']
  4. R.take(4, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz']
  5. R.take(3, 'ramda'); //=> 'ram'
  6. const personnel = [
  7. 'Dave Brubeck',
  8. 'Paul Desmond',
  9. 'Eugene Wright',
  10. 'Joe Morello',
  11. 'Gerry Mulligan',
  12. 'Bob Bates',
  13. 'Joe Dodge',
  14. 'Ron Crotty'
  15. ];
  16. const takeFive = R.take(5);
  17. takeFive(personnel);
  18. //=> ['Dave Brubeck', 'Paul Desmond', 'Eugene Wright', 'Joe Morello', 'Gerry Mulligan']

takeWhile


(a → Boolean) → [a] → [a]

(a → Boolean) → String → String

Parameters

  • fnThe function called per iteration.
  • xsThe collection to iterate over.

Returns Array A new array.

Added in v0.1.0

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.

  1. const isNotFour = x => x !== 4;
  2. R.takeWhile(isNotFour, [1, 2, 3, 4, 3, 2, 1]); //=> [1, 2, 3]
  3. R.takeWhile(x => x !== 'd' , 'Ramda'); //=> 'Ram'

findLast


(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.

Added in v0.1.1

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.

  1. const xs = [{a: 1, b: 0}, {a:1, b: 1}];
  2. R.findLast(R.propEq(1, 'a'))(xs); //=> {a: 1, b: 1}
  3. R.findLast(R.propEq(4, 'a'))(xs); //=> undefined

findLastIndex


(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.

Added in v0.1.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.

  1. const xs = [{a: 1, b: 0}, {a:1, b: 1}];
  2. R.findLastIndex(R.propEq(1, 'a'))(xs); //=> 1
  3. R.findLastIndex(R.propEq(4, 'a'))(xs); //=> -1

forEach


(a → *) → [a] → [a]

Parameters

  • fnThe function to invoke. Receives one argument, value.
  • listThe list to iterate over.

Returns Array The original list.

Added in v0.1.1

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.

  1. const printXPlusFive = x => console.log(x + 5);
  2. R.forEach(printXPlusFive, [1, 2, 3]); //=> [1, 2, 3]
  3. // logs 6
  4. // logs 7
  5. // logs 8

repeat


a → n → [a]

Parameters

  • valueThe value to repeat.
  • nThe desired size of the output list.

Returns Array A new array containing n values.

Added in v0.1.1

Returns a fixed list of size n containing a specified identical value.

See also times.

  1. R.repeat('hi', 5); //=> ['hi', 'hi', 'hi', 'hi', 'hi']
  2. const obj = {};
  3. const repeatedObjs = R.repeat(obj, 5); //=> [{}, {}, {}, {}, {}]
  4. repeatedObjs[0] === repeatedObjs[1]; //=> true

findIndex


(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.

Added in v0.1.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.

See also transduce, indexOf.

  1. const xs = [{a: 1}, {a: 2}, {a: 3}];
  2. R.findIndex(R.propEq(2, 'a'))(xs); //=> 1
  3. R.findIndex(R.propEq(4, 'a'))(xs); //=> -1

last


[a] → a | Undefined

String → String | Undefined

Added in v0.1.4

Returns the last element of the given list or string.

See also init, head, tail.

  1. R.last(['fi', 'fo', 'fum']); //=> 'fum'
  2. R.last([]); //=> undefined
  3. R.last('abc'); //=> 'c'
  4. R.last(''); //=> undefined

partition


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.

Added in v0.1.4

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.

See also filter, reject.

  1. R.partition(R.includes('s'), ['sss', 'ttt', 'foo', 'bars']);
  2. // => [ [ 'sss', 'bars' ], [ 'ttt', 'foo' ] ]
  3. R.partition(R.includes('s'), { a: 'sss', b: 'ttt', foo: 'bars' });
  4. // => [ { a: 'sss', foo: 'bars' }, { b: 'ttt' } ]

slice


Number → Number → [a] → [a]

Number → Number → String → String

Parameters

  • fromIndexThe start index (inclusive).
  • toIndexThe end index (exclusive).
  • list

Returns *

Added in v0.1.4

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.

  1. R.slice(1, 3, ['a', 'b', 'c', 'd']); //=> ['b', 'c']
  2. R.slice(1, Infinity, ['a', 'b', 'c', 'd']); //=> ['b', 'c', 'd']
  3. R.slice(0, -1, ['a', 'b', 'c', 'd']); //=> ['a', 'b', 'c']
  4. R.slice(-3, -1, ['a', 'b', 'c', 'd']); //=> ['b', 'c']
  5. R.slice(0, 3, 'ramda'); //=> 'ram'

uniqWith


((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.

Added in v0.2.0

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.

  1. const strEq = R.eqBy(String);
  2. R.uniqWith(strEq)([1, '1', 2, 1]); //=> [1, 2]
  3. R.uniqWith(strEq)([{}, {}]); //=> [{}]
  4. R.uniqWith(strEq)([1, '1', 1]); //=> [1]
  5. R.uniqWith(strEq)(['1', 1, 1]); //=> ['1']

insert


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.

Added in v0.2.2

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.

  1. R.insert(2, 'x', [1,2,3,4]); //=> [1,2,'x',3,4]

remove


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.

Added in v0.2.2

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.

  1. R.remove(2, 3, [1,2,3,4,5,6,7,8]); //=> [1,2,6,7,8]

times


(Number → a) → Number → [a]

Parameters

  • fnThe function to invoke. Passed one argument, the current value of n.
  • nA value between 0 and n - 1. Increments after each function call.

Returns Array An array containing the return values of all calls to fn.

Added in v0.2.3

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.

  1. R.times(R.identity, 5); //=> [0, 1, 2, 3, 4]

chain


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

Added in v0.3.0

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.

  1. const duplicate = n => [n, n];
  2. R.chain(duplicate, [1, 2, 3]); //=> [1, 1, 2, 2, 3, 3]
  3. R.chain(R.append, R.head)([1, 2, 3]); //=> [1, 2, 3, 1]

fromPairs


[[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.

Added in v0.3.0

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.

See also toPairs, pair.

  1. R.fromPairs([['a', 1], ['b', 2], ['c', 3]]); //=> {a: 1, b: 2, c: 3}

length


[a] → Number

Parameters

  • listThe array to inspect.

Returns Number The length of the array.

Added in v0.3.0

Returns the number of elements in the array by returning list.length.

  1. R.length([]); //=> 0
  2. R.length([1, 2, 3]); //=> 3

unnest


Chain c => c (c a) → c a

Added in v0.3.0

Shorthand for R.chain(R.identity), which removes one level of nesting from any Chain.

See also flatten, chain.

  1. R.unnest([1, [2], [[3]]]); //=> [1, 2, [3]]
  2. R.unnest([[1, 2], [3, 4], [5, 6]]); //=> [1, 2, 3, 4, 5, 6]

zipObj


[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.

Added in v0.3.0

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).

  1. R.zipObj(['a', 'b', 'c'], [1, 2, 3]); //=> {a: 1, b: 2, c: 3}

dropWhile


(a → Boolean) → [a] → [a]

(a → Boolean) → String → String

Parameters

  • fnThe function called per iteration.
  • xsThe collection to iterate over.

Returns Array A new array.

Added in v0.9.0

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.

  1. const lteTwo = x => x <= 2;
  2. R.dropWhile(lteTwo, [1, 2, 3, 4, 3, 2, 1]); //=> [3, 4, 3, 2, 1]
  3. R.dropWhile(x => x !== 'd' , 'Ramda'); //=> 'da'

init


[a] → [a]

String → String

Added in v0.9.0

Returns all but the last element of the given list or string.

See also last, head, tail.

  1. R.init([1, 2, 3]); //=> [1, 2]
  2. R.init([1, 2]); //=> [1]
  3. R.init([1]); //=> []
  4. R.init([]); //=> []
  5. R.init('abc'); //=> 'ab'
  6. R.init('ab'); //=> 'a'
  7. R.init('a'); //=> ''
  8. R.init(''); //=> ''

insertAll


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.

Added in v0.9.0

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.

  1. R.insertAll(2, ['x','y','z'], [1,2,3,4]); //=> [1,2,'x','y','z',3,4]

mapAccum


((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.

Added in v0.10.0

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.

  1. const digits = ['1', '2', '3', '4'];
  2. const appender = (a, b) => [a + b, a + b];
  3. R.mapAccum(appender, 0, digits); //=> ['01234', ['01', '012', '0123', '01234']]

mapAccumRight


((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.

Added in v0.10.0

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].

See also addIndex, mapAccum.

  1. const digits = ['1', '2', '3', '4'];
  2. const appender = (a, b) => [b + a, b + a];
  3. R.mapAccumRight(appender, 5, digits); //=> ['12345', ['12345', '2345', '345', '45']]

mergeAll


[{k: v}] → {k: v}

Parameters

  • listAn array of objects

Returns Object A merged object.

Added in v0.10.0

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.

  1. R.mergeAll([{foo:1},{bar:2},{baz:3}]); //=> {foo:1,bar:2,baz:3}
  2. R.mergeAll([{foo:1},{foo:2},{bar:2}]); //=> {foo:2,bar:2}

scan


((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.

Added in v0.10.0

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.

See also reduce, mapAccum.

  1. const numbers = [1, 2, 3, 4];
  2. const factorials = R.scan(R.multiply, 1, numbers); //=> [1, 1, 2, 6, 24]

unfold


(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 to fn.
  • seedThe seed value.

Returns Array The final list.

Added in v0.10.0

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).

  1. const f = n => n > 50 ? false : [-n, n + 10];
  2. R.unfold(f, 10); //=> [-10, -20, -30, -40, -50]

aperture


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

Added in v0.12.0

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.

  1. R.aperture(2, [1, 2, 3, 4, 5]); //=> [[1, 2], [2, 3], [3, 4], [4, 5]]
  2. R.aperture(3, [1, 2, 3, 4, 5]); //=> [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
  3. R.aperture(7, [1, 2, 3, 4, 5]); //=> []

into


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.

Added in v0.12.0

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.

  1. const numbers = [1, 2, 3, 4];
  2. const transducer = R.compose(R.map(R.add(1)), R.take(2));
  3. R.into([], transducer, numbers); //=> [2, 3]
  4. const intoArray = R.into([]);
  5. intoArray(transducer, numbers); //=> [2, 3]

none


(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.

Added in v0.12.0

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.

See also all, any.

  1. const isEven = n => n % 2 === 0;
  2. const isOdd = n => n % 2 !== 0;
  3. R.none(isEven, [1, 3, 5, 7, 9, 11]); //=> true
  4. R.none(isOdd, [1, 3, 5, 7, 8, 11]); //=> false

transduce


(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.

Added in v0.12.0

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.

  1. const numbers = [1, 2, 3, 4];
  2. const transducer = R.compose(R.map(R.add(1)), R.take(2));
  3. R.transduce(transducer, R.flip(R.append), [], numbers); //=> [2, 3]
  4. const isOdd = (x) => x % 2 !== 0;
  5. const firstOddTransducer = R.compose(R.filter(isOdd), R.take(1));
  6. R.transduce(firstOddTransducer, R.flip(R.append), [], R.range(0, 100)); //=> [1]

adjust


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.

Added in v0.14.0

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.

See also update.

  1. R.adjust(1, R.toUpper, ['a', 'b', 'c', 'd']); //=> ['a', 'B', 'c', 'd']
  2. R.adjust(-1, R.toUpper, ['a', 'b', 'c', 'd']); //=> ['a', 'b', 'c', 'D']

dropRepeats


[a] → [a]

Parameters

  • listThe array to consider.

Returns Array list without repeating elements.

Added in v0.14.0

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.

  1. R.dropRepeats([1, 1, 1, 2, 3, 4, 4, 2, 2]); //=> [1, 2, 3, 4, 2]

dropRepeatsWith


((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.

Added in v0.14.0

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.

  1. const l = [1, -1, 1, 3, 4, -4, -4, -5, 5, 3, 3];
  2. R.dropRepeatsWith(R.eqBy(Math.abs), l); //=> [1, 3, 4, -5, 3]

intersperse


a → [a] → [a]

Parameters

  • separatorThe element to add to the list.
  • listThe list to be interposed.

Returns Array The new list.

Added in v0.14.0

Creates a new list with the separator interposed between elements.

Dispatches to the intersperse method of the second argument, if present.

  1. R.intersperse('a', ['b', 'n', 'n', 's']); //=> ['b', 'a', 'n', 'a', 'n', 'a', 's']

update


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.

Added in v0.14.0

Returns a new copy of the array with the element at the provided index replaced with the given value.

See also adjust.

  1. R.update(1, '_', ['a', 'b', 'c']); //=> ['a', '_', 'c']
  2. R.update(-1, '_', ['a', 'b', 'c']); //=> ['a', 'b', '_']

reduced


a → *

Parameters

  • xThe final value of the reduce.

Returns * The wrapped value.

Added in v0.15.0

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.

  1. R.reduce(
  2. (acc, item) => item > 3 ? R.reduced(acc) : acc.concat(item),
  3. [],
  4. [1, 2, 3, 4, 5]) // [1, 2, 3]

dropLast


Number → [a] → [a]

Number → String → String

Parameters

  • nThe number of elements of list to skip.
  • listThe list of elements to consider.

Returns Array A copy of the list with only the first list.length - n elements

Added in v0.16.0

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.

  1. R.dropLast(1, ['foo', 'bar', 'baz']); //=> ['foo', 'bar']
  2. R.dropLast(2, ['foo', 'bar', 'baz']); //=> ['foo']
  3. R.dropLast(3, ['foo', 'bar', 'baz']); //=> []
  4. R.dropLast(4, ['foo', 'bar', 'baz']); //=> []
  5. R.dropLast(3, 'ramda'); //=> 'ra'

dropLastWhile


(a → Boolean) → [a] → [a]

(a → Boolean) → String → String

Parameters

  • 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.

Added in v0.16.0

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.

  1. const lteThree = x => x <= 3;
  2. R.dropLastWhile(lteThree, [1, 2, 3, 4, 3, 2, 1]); //=> [1, 2, 3, 4]
  3. R.dropLastWhile(x => x !== 'd' , 'Ramda'); //=> 'Ramd'

splitEvery


Number → [a] → [[a]]

Number → String → [String]

Added in v0.16.0

Splits a collection into slices of the specified length.

  1. R.splitEvery(3, [1, 2, 3, 4, 5, 6, 7]); //=> [[1, 2, 3], [4, 5, 6], [7]]
  2. R.splitEvery(3, 'foobarbaz'); //=> ['foo', 'bar', 'baz']

takeLast


Number → [a] → [a]

Number → String → String

Parameters

  • nThe number of elements to return.
  • xsThe collection to consider.

Returns Array

Added in v0.16.0

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.

  1. R.takeLast(1, ['foo', 'bar', 'baz']); //=> ['baz']
  2. R.takeLast(2, ['foo', 'bar', 'baz']); //=> ['bar', 'baz']
  3. R.takeLast(3, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz']
  4. R.takeLast(4, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz']
  5. R.takeLast(3, 'ramda'); //=> 'mda'

takeLastWhile


(a → Boolean) → [a] → [a]

(a → Boolean) → String → String

Parameters

  • fnThe function called per iteration.
  • xsThe collection to iterate over.

Returns Array A new array.

Added in v0.16.0

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.

  1. const isNotOne = x => x !== 1;
  2. R.takeLastWhile(isNotOne, [1, 2, 3, 4]); //=> [2, 3, 4]
  3. R.takeLastWhile(x => x !== 'R' , 'Ramda'); //=> 'amda'

uniqBy


(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.

Added in v0.16.0

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.

  1. R.uniqBy(Math.abs, [-1, -5, 2, 10, 1, 2]); //=> [-1, -5, 2, 10]

pair


a → b → (a,b)

Added in v0.18.0

Takes two arguments, fst and snd, and returns [fst, snd].

See also objOf, of.

  1. R.pair('foo', 'bar'); //=> ['foo', 'bar']