countBy


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

Added in v0.1.0

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.

  1. const numbers = [1.0, 1.1, 1.2, 2.0, 3.0, 2.2];
  2. R.countBy(Math.floor)(numbers); //=> {'1': 3, '2': 2, '3': 1}
  3. const letters = ['a', 'b', 'A', 'a', 'B', 'c'];
  4. R.countBy(R.toLower)(letters); //=> {'a': 3, 'b': 2, 'c': 1}

difference


[*] → [*] → [*]

Parameters

  • list1The first list.
  • list2The second list.

Returns Array The elements in list1 that are not in list2.

Added in v0.1.0

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.

  1. R.difference([1,2,3,4], [7,6,5,4,3]); //=> [1,2]
  2. R.difference([7,6,5,4,3], [1,2,3,4]); //=> [7,6,5]
  3. R.difference([{a: 1}, {b: 2}], [{a: 1}, {c: 3}]) //=> [{b: 2}]

differenceWith


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

Added in v0.1.0

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.

  1. const cmp = (x, y) => x.a === y.a;
  2. const l1 = [{a: 1}, {a: 2}, {a: 3}];
  3. const l2 = [{a: 3}, {a: 4}];
  4. R.differenceWith(cmp, l1, l2); //=> [{a: 1}, {a: 2}]
  5. R.differenceWith(R.equals, [1, 2, 3, 3, 3], []); //=> [1, 2, 3]
  6. R.differenceWith(R.equals, [1, 2, 3, 3, 3], [1]); //=> [2, 3]

unionWith


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

Added in v0.1.0

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.

  1. const l1 = [{a: 1}, {a: 2}];
  2. const l2 = [{a: 1}, {a: 4}];
  3. R.unionWith(R.eqBy(R.prop('a')), l1, l2); //=> [{a: 1}, {a: 2}, {a: 4}]

union


[*] → [*] → [*]

Parameters

  • asThe first list.
  • bsThe second list.

Returns Array The first and second lists concatenated, with duplicates removed.

Added in v0.1.0

Combines two lists into a set (i.e. no duplicates) composed of the elements of each list.

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

gt


Ord a => a → a → Boolean

Added in v0.1.0

Returns true if the first argument is greater than the second; false otherwise.

See also lt.

  1. R.gt(2, 1); //=> true
  2. R.gt(2, 2); //=> false
  3. R.gt(2, 3); //=> false
  4. R.gt('a', 'z'); //=> false
  5. R.gt('z', 'a'); //=> true

gte


Ord a => a → a → Boolean

Added in v0.1.0

Returns true if the first argument is greater than or equal to the second; false otherwise.

See also lte.

  1. R.gte(2, 1); //=> true
  2. R.gte(2, 2); //=> true
  3. R.gte(2, 3); //=> false
  4. R.gte('a', 'z'); //=> false
  5. R.gte('z', 'a'); //=> true

intersection


[*] → [*] → [*]

Parameters

  • list1The first list.
  • list2The second list.

Returns Array The list of elements found in both list1 and list2.

Added in v0.1.0

Combines two lists into a set (i.e. no duplicates) composed of those elements common to both lists.

See also innerJoin.

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

lt


Ord a => a → a → Boolean

Added in v0.1.0

Returns true if the first argument is less than the second; false otherwise.

See also gt.

  1. R.lt(2, 1); //=> false
  2. R.lt(2, 2); //=> false
  3. R.lt(2, 3); //=> true
  4. R.lt('a', 'z'); //=> true
  5. R.lt('z', 'a'); //=> false

lte


Ord a => a → a → Boolean

Added in v0.1.0

Returns true if the first argument is less than or equal to the second; false otherwise.

See also gte.

  1. R.lte(2, 1); //=> false
  2. R.lte(2, 2); //=> true
  3. R.lte(2, 3); //=> true
  4. R.lte('a', 'z'); //=> true
  5. R.lte('z', 'a'); //=> false

max


Ord a => a → a → a

Added in v0.1.0

Returns the larger of its two arguments.

See also maxBy, min.

  1. R.max(789, 123); //=> 789
  2. R.max('a', 'b'); //=> 'b'

min


Ord a => a → a → a

Added in v0.1.0

Returns the smaller of its two arguments.

See also minBy, max.

  1. R.min(789, 123); //=> 123
  2. R.min('a', 'b'); //=> 'a'

propEq


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.

Added in v0.1.0

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.

  1. const abby = {name: 'Abby', age: 7, hair: 'blond'};
  2. const fred = {name: 'Fred', age: 12, hair: 'brown'};
  3. const rusty = {name: 'Rusty', age: 10, hair: 'brown'};
  4. const alois = {name: 'Alois', age: 15, disposition: 'surly'};
  5. const kids = [abby, fred, rusty, alois];
  6. const hasBrownHair = R.propEq('brown', 'hair');
  7. R.filter(hasBrownHair, kids); //=> [fred, rusty]

sortBy


Ord b => (a → b) → [a] → [a]

Parameters

  • fn
  • listThe list to sort.

Returns Array A new list sorted by the keys generated by fn.

Added in v0.1.0

Sorts the list according to the supplied function.

  1. const sortByFirstItem = R.sortBy(R.prop(0));
  2. const pairs = [[-1, 1], [-2, 2], [-3, 3]];
  3. sortByFirstItem(pairs); //=> [[-3, 3], [-2, 2], [-1, 1]]
  4. const sortByNameCaseInsensitive = R.sortBy(R.compose(R.toLower, R.prop('name')));
  5. const alice = {
  6. name: 'ALICE',
  7. age: 101
  8. };
  9. const bob = {
  10. name: 'Bob',
  11. age: -10
  12. };
  13. const clara = {
  14. name: 'clara',
  15. age: 314.159
  16. };
  17. const people = [clara, bob, alice];
  18. sortByNameCaseInsensitive(people); //=> [alice, bob, clara]

pathEq


a → [Idx] → {a} → Boolean

Idx = String | Int | Symbol

Parameters

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

Added in v0.7.0

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.

  1. const user1 = { address: { zipCode: 90210 } };
  2. const user2 = { address: { zipCode: 55555 } };
  3. const user3 = { name: 'Bob' };
  4. const users = [ user1, user2, user3 ];
  5. const isFamous = R.pathEq(90210, ['address', 'zipCode']);
  6. R.filter(isFamous, users); //=> [ user1 ]

maxBy


Ord b => (a → b) → a → a → a

Added in v0.8.0

Takes a function and two values, and returns whichever value produces the larger result when passed to the provided function.

See also max, minBy.

  1. // square :: Number -> Number
  2. const square = n => n * n;
  3. R.maxBy(square, -3, 2); //=> -3
  4. R.reduce(R.maxBy(square), 0, [3, -5, 4, 1, -2]); //=> -5
  5. R.reduce(R.maxBy(square), 0, []); //=> 0

minBy


Ord b => (a → b) → a → a → a

Added in v0.8.0

Takes a function and two values, and returns whichever value produces the smaller result when passed to the provided function.

See also min, maxBy.

  1. // square :: Number -> Number
  2. const square = n => n * n;
  3. R.minBy(square, -3, 2); //=> 2
  4. R.reduce(R.minBy(square), Infinity, [3, -5, 4, 1, -2]); //=> 1
  5. R.reduce(R.minBy(square), Infinity, []); //=> Infinity

equals


a → b → Boolean

Added in v0.15.0

Returns true if its arguments are equivalent, false otherwise. Handles cyclical data structures.

Dispatches symmetrically to the equals methods of both arguments, if present.

  1. R.equals(1, 1); //=> true
  2. R.equals(1, '1'); //=> false
  3. R.equals([1, 2, 3], [1, 2, 3]); //=> true
  4. const a = {}; a.v = a;
  5. const b = {}; b.v = b;
  6. R.equals(a, b); //=> true

identical


a → a → Boolean

Added in v0.15.0

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.

  1. const o = {};
  2. R.identical(o, o); //=> true
  3. R.identical(1, 1); //=> true
  4. R.identical(1, '1'); //=> false
  5. R.identical([], []); //=> false
  6. R.identical(0, -0); //=> false
  7. R.identical(NaN, NaN); //=> true

eqBy


(a → b) → a → a → Boolean

Added in v0.18.0

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.

  1. R.eqBy(Math.abs, 5, -5); //=> true

clamp


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

Added in v0.20.0

Restricts a number to be within a range.

Also works for other ordered types such as Strings and Dates.

  1. R.clamp(1, 10, -5) // => 1
  2. R.clamp(1, 10, 15) // => 10
  3. R.clamp(1, 10, 4) // => 4

sortWith


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

Added in v0.23.0

Sorts a list according to a list of comparators.

See also ascend, descend.

  1. const alice = {
  2. name: 'alice',
  3. age: 40
  4. };
  5. const bob = {
  6. name: 'bob',
  7. age: 30
  8. };
  9. const clara = {
  10. name: 'clara',
  11. age: 40
  12. };
  13. const people = [clara, bob, alice];
  14. const ageNameSort = R.sortWith([
  15. R.descend(R.prop('age')),
  16. R.ascend(R.prop('name'))
  17. ]);
  18. ageNameSort(people); //=> [alice, clara, bob]

innerJoin


((a, b) → Boolean) → [a] → [b] → [a]

Added in v0.24.0

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.

  1. R.innerJoin(
  2. (record, id) => record.id === id,
  3. [{id: 824, name: 'Richie Furay'},
  4. {id: 956, name: 'Dewey Martin'},
  5. {id: 313, name: 'Bruce Palmer'},
  6. {id: 456, name: 'Stephen Stills'},
  7. {id: 177, name: 'Neil Young'}],
  8. [177, 456, 999]
  9. );
  10. //=> [{id: 456, name: 'Stephen Stills'}, {id: 177, name: 'Neil Young'}]