You are currently looking at the v6.0 - v8.2 docs (Reason v3.6 syntax edition). You can find the latest API docs here.
(These docs cover all versions between v3 to v8 and are equivalent to the old BuckleScript docs before the rebrand)
List
Collection functions for manipulating list
data structures.
t('a)
REtype t('a) = list('a);
length
RElet length: t('a) => int;
Returns the length of a list.
REBelt.List.length([1, 2, 3]); /* 3 */
size
RElet size: t('a) => int;
See: length
head
RElet head: t('a) => option('a);
Returns Some(value)
where value
is the first element in the list, or None
if someList
is an empty list.
REBelt.List.head([]); /* None */
Belt.List.head([1, 2, 3]); /* Some(1) */
headExn
RElet headExn: t('a) => 'a;
Same as head, but raises an exception if someList
is empty. Use with care.
REBelt.List.headExn([1, 2, 3]); /* 1 */
Belt.List.headExn([]); /* Raises an Error */
tail
RElet tail: t('a) => option(t('a));
Returns None
if someList
is empty, otherwise it returns Some(tail)
where tail
is everything except the first element of someList
.
REBelt.List.tail([1, 2, 3]); /* Some([2, 3]) */
Belt.List.tail([]); /* None */
tailExn
RElet tailExn: t('a) => t('a);
Same as tail, but raises an exception if someList
is empty. Use with care.
REBelt.List.tailExn([1, 2, 3]); /* [2, 3] */
Belt.List.tailExn([]); /* Raises an Error */
add
RElet add: (t('a), 'a) => t('a);
Adds value
to the beginning of someList
.
REBelt.List.add([2, 3], 1); /* [1, 2, 3] */
Belt.List.add(["World", "!"], "Hello"); /* ["Hello", "World", "!"] */
get
RElet get: (t('a), int) => option('a);
Return the nth element in someList
, or None
if index
is larger than the length.
RElet abc = ["A", "B", "C"];
abc->Belt.List.get(1); /* Some("B") */
abc->Belt.List.get(4); /* None */
getExn
RElet getExn: (t('a), int) => 'a;
Same as get, but raises an exception if index
is larger than the length. Use with care.
RElet abc = ["A", "B", "C"];
abc->Belt.List.getExn(1); /* "B" */
abc->Belt.List.getExn(4); /* Raises an Error */
make
let make: (int, 'a) => t('a);
Returns a list of length numItems
with each element filled with value v
. Returns an empty list if numItems
is negative.
REBelt.List.make(3, 1); /* [1, 1, 1] */
makeBy
RElet makeBy: (int, int => 'a) => t('a);
Return a list of length numItems
with element i
initialized with f(i)
.
Returns an empty list if numItems
is negative.
REBelt.List.makeBy(5, i => i); /* [0, 1, 2, 3, 4] */
Belt.List.makeBy(5, i => i * i); /* [0, 1, 4, 9, 16] */
makeByU
RElet makeByU: (int, [@bs] (int => 'a)) => t('a);
Uncurried version of makeBy
shuffle
RElet shuffle: t('a) => t('a);
Returns a new list in random order.
REBelt.List.shuffle([1, 2, 3]); /* [2, 1, 3] */
drop
RElet drop: (t('a), int) => option(t('a));
Return a new list, dropping the first n
elements. Returns None
if someList
has fewer than n
elements.
RE[1, 2, 3]->Belt.List.drop(2); /* Some([3]) */
[1, 2, 3]->Belt.List.drop(3); /* Some([]) */
[1, 2, 3]->Belt.List.drop(4); /* None */
take
RElet take: (t('a), int) => option(t('a));
Returns a list with the first n
elements from someList
, or None
if someList
has fewer than n
elements.
RE[1, 2, 3]->Belt.List.take(1); /* Some([1]) */
[1, 2, 3]->Belt.List.take(2); /* Some([1, 2]) */
[1, 2, 3]->Belt.List.take(4); /* None */
splitAt
RElet splitAt: (t('a), int) => option((list('a), list('a)));
Split the list someList
at index
. Returns None
when the length of someList
is less than index
.
RE["Hello", "World"]->Belt.List.splitAt(1); /* Some((["Hello"], ["World"])) */
[0, 1, 2, 3, 4]->Belt.List.splitAt(2); /* Some(([0, 1], [2, 3, 4])) */
concat
RElet concat: (t('a), t('a)) => t('a);
Returns the list obtained by adding secondList
after firstList
.
REBelt.List.concat([1, 2, 3], [4, 5]); /* [1, 2, 3, 4, 5] */
concatMany
RElet concatMany: array(t('a)) => t('a);
Returns the list obtained by concatenating all the lists in array a
, in order.
REBelt.List.concatMany([|[1, 2, 3], [], [3], [4]|]); /* [1, 2, 3, 3, 4] */
reverseConcat
RElet reverseConcat: (t('a), t('a)) => t('a);
Equivalent to writing: concat(reverse(firstList), secondList)
REBelt.List.reverseConcat([1, 2], [3, 4]); /* [2, 1, 3, 4] */
flatten
RElet flatten: t(t('a)) => t('a);
Return the list obtained by concatenating all the lists in list ls
, in order.
REBelt.List.flatten([[1, 2, 3], [], [3], [4]]); /* [1, 2, 3, 3, 4] */
map
RElet map: (t('a), 'a => 'b) => t('b);
Returns a new list with f
applied to each element of someList
.
RE[1, 2]->Belt.List.map(x => x + 1); /* [3, 4] */
mapU
RElet mapU: (t('a), [@bs] ('a => 'b)) => t('b);
Uncurried version of map.
zip
RElet zip: (t('a), t('b)) => t(('a, 'b));
Returns a list of pairs from the two lists with the length of the shorter list.
REBelt.List.zip([1, 2], [3, 4, 5]); /* [(1, 3), (2, 4)] */
zipBy
RElet zipBy: (t('a), t('b), ('a, 'b) => 'c) => t('c);
See: zip
Equivalent to:
REzip(firstList, secondList) |> List.map(((x, y)) => f(x, y));
REBelt.List.zipBy([1, 2, 3], [4, 5], (a, b) => 2 * a + b); /* [6, 9] */
zipByU
RElet zipByU: (t('a), t('b), [@bs] (('a, 'b) => 'c)) => t('c);
Uncurried version of zipBy.
mapWithIndex
RElet mapWithIndex: (t('a), (int, 'a) => 'b) => t('b);
Applies f
to each element of someList
.
Function f
takes two arguments: the index starting from 0 and the element from someList
, in that order.
RE[1, 2, 3]->Belt.List.mapWithIndex((index, x) => index + x); /* [1, 3, 5] */
mapWithIndexU
RElet mapWithIndexU: (t('a), [@bs] ((int, 'a) => 'b)) => t('b);
Uncurried version of mapWithIndex.
fromArray
RElet fromArray: array('a) => t('a);
Converts the given array to a list.
REBelt.List.fromArray([|1, 2, 3|]); /* [1, 2, 3] */
toArray
RElet toArray: t('a) => array('a);
Converts the given list to an array.
REBelt.List.toArray([1, 2, 3]); /* [|1, 2, 3|] */
reverse
RElet reverse: t('a) => t('a);
Returns a new list whose elements are those of someList
in reversed order.
REBelt.List.reverse([1, 2, 3]); /* [3, 2, 1] */
mapReverse
RElet mapReverse: (t('a), 'a => 'b) => t('b);
Equivalent to:
REmap(someList, f)->reverse;
RE[3, 4, 5]->Belt.List.mapReverse(x => x * x); /* [25, 16, 9] */
mapReverseU
RElet mapReverseU: (t('a), [@bs] ('a => 'b)) => t('b);
Uncurried version of mapReverse.
forEach
RElet forEach: (t('a), 'a => 'b) => unit;
Call f
on each element of someList
from the beginning to end.
f
returns unit
, so no new array is created. Use forEach
when you are primarily concerned with repetitively creating side effects.
REBelt.List.forEach(["a", "b", "c"], x => Js.log("Item: " ++ x));
/*
prints:
Item: a
Item: b
Item: c
*/
forEachU
RElet forEachU: (t('a), [@bs] ('a => 'b)) => unit;
Uncurried version of forEach.
forEachWithIndex
RElet forEachWithIndex: (t('a), (int, 'a) => 'b) => unit;
Call f
on each element of someList
from beginning to end.
Function f
takes two arguments: the index starting from 0 and the element from someList
. f
returns unit
.
REBelt.List.forEachWithIndex(["a", "b", "c"], (index, x) => Js.log("Item " ++ string_of_int(index) ++ " is " ++ x));
/* prints:
Item 0 is a
Item 1 is b
Item 2 is cc
*/
forEachWithIndexU
RElet forEachWithIndexU: (t('a), [@bs] ((int, 'a) => 'b)) => unit;
Uncurried version of forEachWithIndex.
reduce
RElet reduce: (t('a), 'b, ('b, 'a) => 'b) => 'b;
Applies f
to each element of someList
from beginning to end. Function f
has two parameters: the item from the list and an “accumulator”, which starts with a value of initialValue
. reduce returns the final value of the accumulator.
RE[1, 2, 3, 4]->Belt.List.reduce(0, (+)); /* 10 */
/* same as */
[1, 2, 3, 4]->Belt.List.reduce(0, (acc, item) => acc + item); /* 10 */
reduceU
RElet reduceU: (t('a), 'b, [@bs] (('b, 'a) => 'b)) => 'b;
Uncurried version of reduce.
reduceWithIndex
RElet reduceWithIndex: (t('a), 'b, ('b, 'a, int) => 'b) => 'b;
Applies f
to each element of someList
from beginning to end. Function f
has three parameters: the item from the list and an “accumulator”, which starts with a value of initialValue
and the index of each element. reduceWithIndex
returns the final value of the accumulator.
RE[1, 2, 3, 4]->Belt.List.reduceWithIndex(0, (acc, item, index) => acc + item + index); /* 16 */
reduceWithIndexU
RElet reduceWithIndexU: (t('a), 'b, [@bs] (('b, 'a, int) => 'b)) => 'b;
Uncurried version of reduceWithIndex.
reduceReverse
RElet reduceReverse: (t('a), 'b, ('b, 'a) => 'b) => 'b;
Works like reduce, except that function f
is applied to each item of someList
from the last back to the first.
RE[1, 2, 3, 4]->Belt.List.reduceReverse(0, (+)); /* 10 */
[1, 2, 3, 4]->Belt.List.reduceReverse(10, (-)); /* 0 */
[1, 2, 3, 4]->Belt.List.reduceReverse([], Belt.List.add); /* [1, 2, 3, 4] */
reduceReverseU
RElet reduceReverseU: (t('a), 'b, [@bs] (('b, 'a) => 'b)) => 'b;
Uncurried version of reduceReverse.
mapReverse2
RElet mapReverse2: (t('a), t('b), ('a, 'b) => 'c) => t('c);
Equivalent to: zipBy(xs, ys, f)->reverse
RE
Belt.List.mapReverse2([1, 2, 3], [1, 2], (+)); /* [4, 2] */
mapReverse2U
RElet mapReverse2U: (t('a), t('b), [@bs] (('a, 'b) => 'c)) => t('c);
Uncurried version of mapReverse2.
forEach2
RElet forEach2: (t('a), t('b), ('a, 'b) => 'c) => unit;
Stops at the length of the shorter list.
REBelt.List.forEach2(["Z", "Y"], ["A", "B", "C"], (x, y) => Js.log2(x, y));
/* prints:
"Z" "A"
"Y" "B"
*/
forEach2U
RElet forEach2U: (t('a), t('b), (.'a, 'b) => 'c) => unit;
Uncurried version of forEach2.
reduce2
RElet reduce2: (t('b), t('c), 'a, ('a, 'b, 'c) => 'a) => 'a;
Applies f
to each element of firstList
and secondList
from beginning to end. Stops with the shorter list. Function f
has three parameters: an “accumulator” which starts with a value of initialValue
, an item from firstList
, and an item from secondList
. reduce2
returns the final value of the accumulator.
REBelt.List.reduce2([1, 2, 3], [4, 5], 0, (acc, x, y) => acc + x * x + y); /* 0 + (1 * 1 + 4) + (2 * 2 + 5) */
reduce2U
RElet reduce2U: (t('b), t('c), 'a, (.'a, 'b, 'c) => 'a) => 'a;
Uncurried version of reduce2.
reduceReverse2
RElet reduceReverse2: (t('a), t('b), 'c, ('c, 'a, 'b) => 'c) => 'c;
Applies f
to each element of firstList
and secondList
from end to beginning. Stops with the shorter list. Function f
has three parameters: an “accumulator” which starts with a value of init, an item from firstList
, and an item from secondList
. reduce2
returns the final value of the accumulator.
REBelt.List.reduceReverse2([1, 2, 3], [4, 5], 0, (acc, x, y) => acc + x * x + y); /* + (1 * 1 + 4) + (2 * 2 + 5) */
reduceReverse2U
RElet reduceReverse2U: (t('a), t('b), 'c, (.'c, 'a, 'b) => 'c) => 'c;
Uncurried version of reduceReverse2.
every
RElet every: (t('a), 'a => bool) => bool;
Returns true
if all elements satisfy pred
, where pred
is a predicate: a function taking an element and returning a bool.
RElet isBelow10 = value => value < 10;
[1, 9, 8, 2]->Belt.List.every(isBelow10); /* true */
[1, 99, 8, 2]->Belt.List.every(isBelow10); /* false */
everyU
RElet everyU: (t('a), (.'a) => bool) => bool;
Uncurried version of every.
some
RElet some: (t('a), 'a => bool) => bool;
Returns true
if at least one of the elements in someList
satisfies pred
, where pred
is a predicate: a function taking an element and returning a bool.
RElet isAbove100 = value => value > 100;
[101, 1, 2, 3]->Belt.List.some(isAbove100); /* true */
[1, 2, 3, 4]->Belt.List.some(isAbove100); /* false */
someU
RElet someU: (t('a), (.'a => bool)) => bool;
Uncurried version of some.
every2
RElet every2: (t('a), t('b), ('a, 'b) => bool) => bool;
Returns true
if predicate pred(a, b)
is true
for all pairs of elements up to the shorter length (i.e. min(length(firstList), length(secondList))
)
REBelt.List.every2([1, 2, 3], [0, 1], (>)); /* true */
Belt.List.every2([], [1], (a, b) => a > b); /* true */
Belt.List.every2([2, 3], [1], (a, b) => a > b); /* true */
Belt.List.every2([0, 1], [5, 0], (a, b) => a > b); /* false */
every2U
RElet every2U: (t('a), t('b), [@bs] (('a, 'b) => bool)) => bool;
Uncurried version of every2.
some2
RElet some2: (t('a), t('b), ('a, 'b) => bool) => bool;
Returns true
if predicate pred(a, b)
is true for any pair of elements up to the shorter length (i.e. min(length(firstList), length(secondList))
)
REBelt.List.some2([1, 2, 3], [0, 1], (>)); /* true */
Belt.List.some2([], [1], (a, b) => a > b); /* false */
Belt.List.some2([2, 3], [1], (a, b) => a > b); /* true */
Belt.List.some2([0, 1], [5, 0], (a, b) => a > b); /* true */
some2U
RElet some2U: (t('a), t('b), [@bs] (('a, 'b) => bool)) => bool;
Uncurried version of some2.
cmpByLength
RElet cmpByLength: (t('a), t('a)) => int;
REcmpByLength(firstList, secondList);
Compare two lists solely by length. Returns -1
if length(firstList)
is less than length(secondList)
, 0
if length(firstList)
equals length(secondList)
, and 1
if length(firstList)
is greater than length(secondList)
.
REBelt.List.cmpByLength([1, 2], [3, 4, 5, 6]); /* (-1) */
Belt.List.cmpByLength([1, 2, 3], [4, 5, 6]); /* = 0 */
Belt.List.cmpByLength([1, 2, 3, 4], [5, 6]); /* = 1 */
cmp
RElet cmp: (t('a), t('a), ('a, 'a) => int) => int;
Compare elements one by one compareFn(a, b)
. compareFn
returns a negative number if a
is "less than" b
, zero if a
is "equal to" b
, a positive number if a
is "greater than" b
.
The comparison returns the first non-zero result of compareFn
, or zero if compareFn
returns zero for all a
and b
.
If all items have compared equal, but firstList
is exhausted first, return -1
. (firstList
is shorter).
If all items have compared equal, but secondList
is exhausted first, return 1
(firstList
is longer).
REBelt.List.cmp([3], [3, 7], (a, b) => compare(a, b)); /* (-1) */
Belt.List.cmp([5, 3], [5], (a, b) => compare(a, b)); /* 1 */
Belt.List.cmp([1, 3, 5], [1, 4, 2], (a, b) => compare(a, b)); /* (-1) */
Belt.List.cmp([1, 3, 5], [1, 2, 3], (a, b) => compare(a, b)); /* 1 */
Belt.List.cmp([1, 3, 5], [1, 3, 5], (a, b) => compare(a, b)); /* 0 */
Please note: The total ordering of List is different from Array, for Array, we compare the length first and, only if the lengths are equal, elements one by one. For lists, we just compare elements one by one.
cmpU
RElet cmpU: (t('a), t('a), [@bs] (('a, 'a) => int)) => int;
Uncurried version of cmp.
eq
RElet eq: (t('a), t('a), ('a, 'a) => bool) => bool;
Check equality of firstList
and secondList
using eqElem
for equality on elements, where eqElem
is a function that returns true
if items x
and y
meet some criterion for equality, false
otherwise. eq false
if length of firstList
and secondList
are not the same.
REBelt.List.eq([1, 2, 3], [1, 2], (==)); /* false */
Belt.List.eq([1, 2], [1, 2], (==)); /* true */
Belt.List.eq([1, 2, 3], [(-1), (-2), (-3)], (a, b) => abs(a) == abs(b)); /* true */
eqU
RElet eqU: (t('a), t('a), [@bs] (('a, 'a) => bool)) => bool;
Uncurried version of eqU.
has
RElet has: (t('a), 'b, ('a, 'b) => bool) => bool;
Returns true
if the list contains at least one element for which eqFunction(x)
returns true.
RE[1, 2, 3]->Belt.List.has(2, (==)); /* true */
[1, 2, 3]->Belt.List.has(4, (==)); /* false */
[(-1), (-2), (-3)]->Belt.List.has(2, (a, b) => abs(a) == abs(b)); /* true */
hasU
RElet hasU: (t('a), 'b, [@bs] (('a, 'b) => bool)) => bool;
Uncurried version of has.
getBy
RElet getBy: (t('a), 'a => bool) => option('a);
Returns Some(value)
for the first value in someList
that satisfies the predicate function pred
. Returns None
if no element satisfies the function.
REBelt.List.getBy([1, 4, 3, 2], x => x > 3); /* Some(4) */
Belt.List.getBy([1, 4, 3, 2], x => x > 4); /* None */
getByU
RElet getByU: (t('a), [@bs] ('a => bool)) => option('a);
Uncurried version of getBy.
keep
RElet keep: (t('a), 'a => bool) => t('a);
Returns a list of all elements in someList
which satisfy the predicate function pred
.
RElet isEven = x => x mod 2 == 0;
Belt.List.keep([1, 2, 3, 4], isEven); /* [2, 4] */
Belt.List.keep([None, Some(2), Some(3), None], Belt.Option.isSome); /* [Some(2), Some(3)] */
keepU
RElet keepU: (t('a), [@bs] ('a => bool)) => t('a);
Uncurried version of keep.
filter
RElet filter: (t('a), 'a => bool) => t('a);
Returns a list of all elements in someList
which satisfy the predicate function pred
.
RElet isEven = x => x mod 2 == 0;
Belt.List.filter([1, 2, 3, 4], isEven); /* [2, 4] */
Belt.List.filter([None, Some(2), Some(3), None], Belt.Option.isSome); /* [Some(2), Some(3)] */
keepWithIndex
RElet keepWithIndex: (t('a), ('a, int) => bool) => t('a);
Returns a list of all elements in someList
which satisfy the predicate function pred
.
RElet isEven = x => x mod 2 == 0;
Belt.List.keepWithIndex([1, 2, 3, 4], (_x, index) => isEven(index)); /* [1, 3] */
keepWithIndexU
RElet keepWithIndexU: (t('a), [@bs] (('a, int) => bool)) => t('a);
Uncurried version of keepWithIndex.
filterWithIndex
RElet filterWithIndex: (t('a), ('a, int) => bool) => t('a);
Returns a list of all elements in someList
which satisfy the predicate function pred
.
RElet isEven = x => x mod 2 == 0;
Belt.List.filterWithIndex([1, 2, 3, 4], (_x, index) => isEven(index)); /* [1, 3] */
keepMap
RElet keepMap: (t('a), 'a => option('b)) => t('b);
Applies f
to each element of someList
. If f(x)
returns Some(value)
, then value
is kept in the resulting list.
If f(x)
returns None
, the element is not retained in the result.
RElet isEven = x => x mod 2 == 0;
[1, 2, 3, 4]
->Belt.List.keepMap(x =>
if (isEven(x)) {
Some(x);
} else {
None;
}
); /* [2, 4] */
[Some(1), Some(2), None]->Belt.List.keepMap(x => x); /* [1, 2] */
keepMapU
RElet keepMapU: (t('a), [@bs] ('a => option('b))) => t('b);
Uncurried version of keepMap.
partition
RElet partition: (t('a), 'a => bool) => (t('a), t('a));
Creates a pair of lists; the first list consists of all elements of someList
that satisfy the predicate function pred
; the second list consists of all elements of someList
that do not satisfy `pred.
In other words:
([elementsThatSatisfies], [elementsThatDoesNotSatisfy]);
REBelt.List.partition([1, 2, 3, 4], x => x > 2); /* ([3, 4], [1, 2]) */
partitionU
RElet partitionU: (t('a), [@bs] ('a => bool)) => (t('a), t('a));
Uncurried version of partition.
unzip
RElet unzip: t(('a, 'b)) => (t('a), t('b));
Takes a list of pairs and creates a pair of lists. The first list contains all the first items of the pairs; the second list contains all the second items.
REBelt.List.unzip([(1, 2), (3, 4)]); /* ([1, 3], [2, 4]) */
Belt.List.unzip([("H", "W"), ("e", "o"), ("l", "r"), ("l", "l"), ("o", "d"), (" ", "!")]);
/* (["H", "e", "l", "l", "o", " "], ["W", "o", "r", "l", "d", "!"]); */
getAssoc
RElet getAssoc: (t(('a, 'c)), 'b, ('a, 'b) => bool) => option('c);
Return the second element of a pair in someList
where the first element equals k
as per the predicate function eqFunction
, or None
if not found.
RE[(1, "a"), (2, "b"), (3, "c")]->Belt.List.getAssoc(3, (==)); /* Some("c") */
[(9, "morning"), (15, "afternoon"), (22, "night")]
->Belt.List.getAssoc(15, (k, item) => k /* 15 */ == item /* 9, 5, 22 */);
/* Some("afternoon"); */
getAssocU
RElet getAssocU: (t(('a, 'c)), 'b, [@bs] (('a, 'b) => bool)) => option('c);
Uncurried version of getAssoc.
hasAssoc
RElet hasAssoc: (t(('a, 'c)), 'b, ('a, 'b) => bool) => bool;
Returns true
if there is a pair in someList
where the first element equals k
as per the predicate function eqFunction
.
RE[(1, "a"), (2, "b"), (3, "c")]->Belt.List.hasAssoc(1, (==)); /* true */
[(9, "morning"), (15, "afternoon"), (22, "night")]
->Belt.List.hasAssoc(25, (k, item) => k /* 25 */ == item /* 9, 5, 22 */); /* false */
hasAssocU
RElet hasAssocU: (t(('a, 'c)), 'b, [@bs] (('a, 'b) => bool)) => bool;
Uncurried version of hasAssoc.
removeAssoc
RElet removeAssoc: (t(('a, 'c)), 'b, ('a, 'b) => bool) => t(('a, 'c));
Return a list after removing the first pair whose first value is k
per the equality predicate eqFunction
; if not found, return a new list identical to someList
.
RE[(1, "a"), (2, "b"), (3, "c")]->Belt.List.removeAssoc(1, (==)); /* [(2, "b"), (3, "c")] */
[(9, "morning"), (15, "afternoon"), (22, "night")]
->Belt.List.removeAssoc(9, (k, item) => k /* 9 */ == item /* 9, 5, 22 */);
/* [(15, "afternoon"), (22, "night")] */
removeAssocU
RElet removeAssocU: (t(('a, 'c)), 'b, [@bs] (('a, 'b) => bool)) => t(('a, 'c));
Uncurried version of removeAssoc.
setAssoc
RElet setAssoc: (t(('a, 'c)), 'a, 'c, ('a, 'a) => bool) => t(('a, 'c));
If k
exists in someList
by satisfying the eqFunction
predicate, return a new list with the key and value replaced by the new k
and v
; otherwise, return a new list with the pair k
, v
added to the head of someList
.
RE[(1, "a"), (2, "b"), (3, "c")]->Belt.List.setAssoc(2, "x", (==)); /* [(1, "a"), (2, "x"), (3, "c")]; */
[(1, "a"), (3, "c")]->Belt.List.setAssoc(2, "b", (==)); /* [(2, "b"), (1, "a"), (3, "c")] */
[(9, "morning"), (3, "morning?!"), (22, "night")]
->Belt.List.setAssoc(15, "afternoon", (a, b) => a mod 12 == b mod 12);
/* [(9, "morning"), (15, "afternoon"), (22, "night")] */
Please note
In the last example, since: 15 mod 12
equals 3 mod 12
Both the key and the value are replaced in the list.
setAssocU
RElet setAssocU: (t(('a, 'c)), 'a, 'c, [@bs] (('a, 'a) => bool)) => t(('a, 'c));
Uncurried version of setAssoc.
sort
RElet sort: (t('a), ('a, 'a) => int) => t('a);
Returns a sorted list.
REBelt.List.sort([5, 4, 9, 3, 7], (a, b) => a - b); /* [3, 4, 5, 7, 9] */
sortU
RElet sortU: (t('a), [@bs] (('a, 'a) => int)) => t('a);
Uncurried version of sort.