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]