Belt_List
Collection functions for manipulating the list
data structures, a singly-linked list.
Prefer Array if you need any of the following:
Random access of element
Better interop with JavaScript
Better memory usage & performance.
t
'a t
is compatible with built-in list
type
type t<'a> = list<'a>
length
Returns the length of a list.
RESBelt.List.length(list{1, 2, 3}) // 3
let length: t<'a> => int
size
See length
let size: t<'a> => int
head
Returns Some(value)
where value
is the first element in the list, or
None
if someList
is an empty list.
RESBelt.List.head(list{}) // None
Belt.List.head(list{1, 2, 3}) // Some(1)
let head: t<'a> => option<'a>
headExn
Same as head, but raises an exception if someList
is empty. Use
with care.
RESBelt.List.headExn(list{1, 2, 3}) // 1
Belt.List.headExn(list{}) // Raises an Error
let headExn: t<'a> => 'a
tail
Returns None
if someList
is empty, otherwise it returns Some(tail)
where tail
is everything except the first element of someList
.
RESBelt.List.tail(list{1, 2, 3}) // Some(list{2, 3})
Belt.List.tail(list{}) // None
let tail: t<'a> => option<t<'a>>
tailExn
Same as tail, but raises an exception if someList
is empty. Use
with care.
RESBelt.List.tailExn(list{1, 2, 3}) // list{2, 3}
Belt.List.tailExn(list{}) // Raises an Error
let tailExn: t<'a> => t<'a>
add
Adds value
to the beginning of someList
.
RESBelt.List.add(list{2, 3}, 1) // list{1, 2, 3}
Belt.List.add(list{"World", "!"}, "Hello") // list{"Hello", "World", "!"}
let add: (t<'a>, 'a) => t<'a>
get
Return the nth element in someList
, or None
if index
is larger than the
length.
RESlet abc = list{"A", "B", "C"}
abc->Belt.List.get(1) // Some("B")
abc->Belt.List.get(4) // None
let get: (t<'a>, int) => option<'a>
getExn
Same as get, but raises an exception if index
is larger than the
length. Use with care.
RESlet abc = list{"A", "B", "C"}
abc->Belt.List.getExn(1) // "B"
abc->Belt.List.getExn(4) // Raises an Error
let getExn: (t<'a>, int) => 'a
make
Returns a list of length numItems
with each element filled with value v
. Returns an empty list if numItems
is negative.
RESBelt.List.make(3, 1) // list{1, 1, 1}
let make: (int, 'a) => t<'a>
makeByU
Uncurried version of makeBy
let makeByU: (int, (. int) => 'a) => t<'a>
makeBy
Return a list of length numItems
with element i
initialized with f(i)
.
Returns an empty list if numItems
is negative.
RESBelt.List.makeBy(5, i => i) // list{0, 1, 2, 3, 4}
Belt.List.makeBy(5, i => i * i) // list{0, 1, 4, 9, 16}
let makeBy: (int, int => 'a) => t<'a>
shuffle
Returns a new list in random order.
RESBelt.List.shuffle(list{1, 2, 3}) // list{2, 1, 3}
let shuffle: t<'a> => t<'a>
drop
Return a new list, dropping the first n
elements. Returns None
if someList
has fewer than n
elements.
RESlist{1, 2, 3}->Belt.List.drop(2) // Some(list{3})
list{1, 2, 3}->Belt.List.drop(3) // Some(list{})
list{1, 2, 3}->Belt.List.drop(4) // None
let drop: (t<'a>, int) => option<t<'a>>
take
Returns a list with the first n
elements from someList
, or None
if someList
has fewer than n
elements.
RESlist{1, 2, 3}->Belt.List.take(1) // Some(list{1})
list{1, 2, 3}->Belt.List.take(2) // Some(list{1, 2})
list{1, 2, 3}->Belt.List.take(4) // None
let take: (t<'a>, int) => option<t<'a>>
splitAt
Split the list someList
at index
. Returns None
when the length of someList
is less than index
.
RESlist{"Hello", "World"}->Belt.List.splitAt(1) // Some((list{"Hello"}, list{"World"}))
list{0, 1, 2, 3, 4}->Belt.List.splitAt(2) // Some((list{0, 1}, list{2, 3, 4}))
let splitAt: (t<'a>, int) => option<(list<'a>, list<'a>)>
concat
Returns the list obtained by adding secondList
after firstList
.
RESBelt.List.concat(list{1, 2, 3}, list{4, 5}) // list{1, 2, 3, 4, 5}
let concat: (t<'a>, t<'a>) => t<'a>
concatMany
Returns the list obtained by concatenating all the lists in array a
, in
order.
RESBelt.List.concatMany([list{1, 2, 3}, list{}, list{3}]) // list{1, 2, 3, 3}
let concatMany: array<t<'a>> => t<'a>
reverseConcat
Equivalent to writing: concat(reverse(firstList, secondList)
RESBelt.List.reverseConcat(list{1, 2}, list{3, 4}) // list{2, 1, 3, 4}
let reverseConcat: (t<'a>, t<'a>) => t<'a>
flatten
Return the list obtained by concatenating all the lists in list ls
, in order.
RESBelt.List.flatten(list{list{1, 2, 3}, list{}, list{3}}) // list{1, 2, 3, 3}
let flatten: t<t<'a>> => t<'a>
mapU
Uncurried version of map.
let mapU: (t<'a>, (. 'a) => 'b) => t<'b>
map
Returns a new list with f
applied to each element of someList
.
RESlist{1, 2}->Belt.List.map(x => x + 1) // list{3, 4}
let map: (t<'a>, 'a => 'b) => t<'b>
zip
Returns a list of pairs from the two lists with the length of the shorter list.
RESBelt.List.zip(list{1, 2}, list{3, 4, 5}) // list{(1, 3), (2, 4)}
let zip: (t<'a>, t<'b>) => t<('a, 'b)>
zipByU
Uncurried version of zipBy.
let zipByU: (t<'a>, t<'b>, (. 'a, 'b) => 'c) => t<'c>
zipBy
See: zip
RESBelt.List.zipBy(list{1, 2, 3}, list{4, 5}, (a, b) => 2 * a + b) // list{6, 9}
let zipBy: (t<'a>, t<'b>, ('a, 'b) => 'c) => t<'c>
mapWithIndexU
Uncurried version of mapWithIndex.
let mapWithIndexU: (t<'a>, (. int, 'a) => 'b) => t<'b>
mapWithIndex
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.
RESlist{1, 2, 3}->Belt.List.mapWithIndex((index, x) => index + x) // list{1, 3, 5}
let mapWithIndex: (t<'a>, (int, 'a) => 'b) => t<'b>
fromArray
Converts the given array to a list.
RESBelt.List.fromArray([1, 2, 3]) // list{1, 2, 3}
let fromArray: array<'a> => t<'a>
toArray
Converts the given list to an array.
RESBelt.List.toArray(list{1, 2, 3}) // [1, 2, 3]
let toArray: t<'a> => array<'a>
reverse
Returns a new list whose elements are those of someList
in reversed order.
RESBelt.List.reverse(list{1, 2, 3}) /* list{3, 2, 1} */
let reverse: t<'a> => t<'a>
mapReverseU
Uncurried version of mapReverse.
let mapReverseU: (t<'a>, (. 'a) => 'b) => t<'b>
mapReverse
Equivalent to:
RESmap(someList, f)->reverse
RESlist{3, 4, 5}->Belt.List.mapReverse(x => x * x) /* list{25, 16, 9} */
let mapReverse: (t<'a>, 'a => 'b) => t<'b>
forEachU
Uncurried version of forEach.
let forEachU: (t<'a>, (. 'a) => 'b) => unit
forEach
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.
RESBelt.List.forEach(list{"a", "b", "c"}, x => Js.log("Item: " ++ x))
/*
prints:
Item: a
Item: b
Item: c
*/
let forEach: (t<'a>, 'a => 'b) => unit
forEachWithIndexU
Uncurried version of forEachWithIndex.
let forEachWithIndexU: (t<'a>, (. int, 'a) => 'b) => unit
forEachWithIndex
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
.
RESBelt.List.forEachWithIndex(list{"a", "b", "c"}, (index, x) => {
Js.log("Item " ++ Belt.Int.toString(index) ++ " is " ++ x)
})
/*
prints:
Item 0 is a
Item 1 is b
Item 2 is cc
*/
let forEachWithIndex: (t<'a>, (int, 'a) => 'b) => unit
reduceU
Uncurried version of reduce.
let reduceU: (t<'a>, 'b, (. 'b, 'a) => 'b) => 'b
reduce
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.
RESlist{1, 2, 3, 4}->Belt.List.reduce(0, (a, b) => a + b) /* 10 */
/* same as */
list{1, 2, 3, 4}->Belt.List.reduce(0, (acc, item) => acc + item) /* 10 */
let reduce: (t<'a>, 'b, ('b, 'a) => 'b) => 'b
reduceWithIndexU
Uncurried version of reduceWithIndex.
let reduceWithIndexU: (t<'a>, 'b, (. 'b, 'a, int) => 'b) => 'b
reduceWithIndex
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.
RESlist{1, 2, 3, 4}->Belt.List.reduceWithIndex(0, (acc, item, index) => acc + item + index) /* 16 */
let reduceWithIndex: (t<'a>, 'b, ('b, 'a, int) => 'b) => 'b
reduceReverseU
Uncurried version of reduceReverse.
let reduceReverseU: (t<'a>, 'b, (. 'b, 'a) => 'b) => 'b
reduceReverse
Works like reduce, except that function f
is applied to each
item of someList
from the last back to the first.
RESlist{1, 2, 3, 4}->Belt.List.reduceReverse(0, (a, b) => a + b) /* 10 */
list{1, 2, 3, 4}->Belt.List.reduceReverse(10, (a, b) => a - b) /* 0 */
list{1, 2, 3, 4}->Belt.List.reduceReverse(list{}, Belt.List.add) // list{1, 2, 3, 4}
let reduceReverse: (t<'a>, 'b, ('b, 'a) => 'b) => 'b
mapReverse2U
Uncurried version of mapReverse2.
let mapReverse2U: (t<'a>, t<'b>, (. 'a, 'b) => 'c) => t<'c>
mapReverse2
Equivalent to: zipBy(xs, ys, f)->reverse
RES
Belt.List.mapReverse2(list{1, 2, 3}, list{1, 2}, (a, b) => a + b) // list{4, 2}
let mapReverse2: (t<'a>, t<'b>, ('a, 'b) => 'c) => t<'c>
forEach2U
Uncurried version of forEach2.
let forEach2U: (t<'a>, t<'b>, (. 'a, 'b) => 'c) => unit
forEach2
Stops at the length of the shorter list.
RESBelt.List.forEach2(list{"Z", "Y"}, list{"A", "B", "C"}, (x, y) => Js.log2(x, y))
/*
prints:
"Z" "A"
"Y" "B"
*/
let forEach2: (t<'a>, t<'b>, ('a, 'b) => 'c) => unit
reduce2U
Uncurried version of reduce2.
let reduce2U: (t<'b>, t<'c>, 'a, (. 'a, 'b, 'c) => 'a) => 'a
reduce2
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.
RESBelt.List.reduce2(list{1, 2, 3}, list{4, 5}, 0, (acc, x, y) => acc + x * x + y) /* 0 + (1 * 1 + 4) + (2 * 2 + 5) */
let reduce2: (t<'b>, t<'c>, 'a, ('a, 'b, 'c) => 'a) => 'a
reduceReverse2U
Uncurried version of reduceReverse2.
let reduceReverse2U: (t<'a>, t<'b>, 'c, (. 'c, 'a, 'b) => 'c) => 'c
reduceReverse2
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.
RESBelt.List.reduceReverse2(list{1, 2, 3}, list{4, 5}, 0, (acc, x, y) => acc + x * x + y) /* + (1 * 1 + 4) + (2 * 2 + 5) */
let reduceReverse2: (t<'a>, t<'b>, 'c, ('c, 'a, 'b) => 'c) => 'c
everyU
Uncurried version of every.
let everyU: (t<'a>, (. 'a) => bool) => bool
every
Returns true
if all elements satisfy pred
, where pred
is a predicate: a function taking an element and returning a bool.
RESlet isBelow10 = value => value < 10
list{1, 9, 8, 2}->Belt.List.every(isBelow10) /* true */
list{1, 99, 8, 2}->Belt.List.every(isBelow10) /* false */
let every: (t<'a>, 'a => bool) => bool
someU
Uncurried version of some.
let someU: (t<'a>, (. 'a) => bool) => bool
some
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.
RESlet isAbove100 = value => value > 100
list{101, 1, 2, 3}->Belt.List.some(isAbove100) /* true */
list{1, 2, 3, 4}->Belt.List.some(isAbove100) /* false */
let some: (t<'a>, 'a => bool) => bool
every2U
Uncurried version of every2.
let every2U: (t<'a>, t<'b>, (. 'a, 'b) => bool) => bool
every2
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))
)
RESBelt.List.every2(list{1, 2, 3}, list{0, 1}, (a, b) => a > b) /* true */
Belt.List.every2(list{}, list{1}, (a, b) => a > b) /* true */
Belt.List.every2(list{2, 3}, list{1}, (a, b) => a > b) /* true */
Belt.List.every2(list{0, 1}, list{5, 0}, (a, b) => a > b) /* false */
let every2: (t<'a>, t<'b>, ('a, 'b) => bool) => bool
some2U
Uncurried version of some2.
let some2U: (t<'a>, t<'b>, (. 'a, 'b) => bool) => bool
some2
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))
)
RESBelt.List.some2(list{1, 2, 3}, list{0, 1}, (a, b) => a > b) /* true */
Belt.List.some2(list{}, list{1}, (a, b) => a > b) /* false */
Belt.List.some2(list{2, 3}, list{1}, (a, b) => a > b) /* true */
Belt.List.some2(list{0, 1}, list{5, 0}, (a, b) => a > b) /* true */
let some2: (t<'a>, t<'b>, ('a, 'b) => bool) => bool
cmpByLength
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)
.
RESBelt.List.cmpByLength(list{1, 2}, list{3, 4, 5, 6}) /* -1 */
Belt.List.cmpByLength(list{1, 2, 3}, list{4, 5, 6}) /* = 0 */
Belt.List.cmpByLength(list{1, 2, 3, 4}, list{5, 6}) /* = 1 */
let cmpByLength: (t<'a>, t<'a>) => int
cmpU
Uncurried version of cmp.
let cmpU: (t<'a>, t<'a>, (. 'a, 'a) => int) => int
cmp
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).
RESBelt.List.cmp(list{3}, list{3, 7}, (a, b) => compare(a, b)) /* (-1) */
Belt.List.cmp(list{5, 3}, list{5}, (a, b) => compare(a, b)) /* 1 */
Belt.List.cmp(list{1, 3, 5}, list{1, 4, 2}, (a, b) => compare(a, b)) /* (-1) */
Belt.List.cmp(list{1, 3, 5}, list{1, 2, 3}, (a, b) => compare(a, b)) /* 1 */
Belt.List.cmp(list{1, 3, 5}, list{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.
let cmp: (t<'a>, t<'a>, ('a, 'a) => int) => int
eqU
Uncurried version of eq.
let eqU: (t<'a>, t<'a>, (. 'a, 'a) => bool) => bool
eq
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.
RESBelt.List.eq(list{1, 2, 3}, list{1, 2}, (a, b) => a == b) /* false */
Belt.List.eq(list{1, 2}, list{1, 2}, (a, b) => a == b) /* true */
Belt.List.eq(list{1, 2, 3}, list{(-1), (-2), (-3)}, (a, b) => abs(a) == abs(b)) /* true */
let eq: (t<'a>, t<'a>, ('a, 'a) => bool) => bool
hasU
Uncurried version of has.
let hasU: (t<'a>, 'b, (. 'a, 'b) => bool) => bool
has
Returns true
if the list contains at least one element for which
eqFunction(x)
returns true.
RESlist{1, 2, 3}->Belt.List.has(2, (a, b) => a == b) /* true */
list{1, 2, 3}->Belt.List.has(4, (a, b) => a == b) /* false */
list{(-1), (-2), (-3)}->Belt.List.has(2, (a, b) => abs(a) == abs(b)) /* true */
let has: (t<'a>, 'b, ('a, 'b) => bool) => bool
getByU
Uncurried version of getBy.
let getByU: (t<'a>, (. 'a) => bool) => option<'a>
getBy
Returns Some(value)
for the first value in someList
that satisfies the
predicate function pred
. Returns None
if no element satisfies the function.
RESBelt.List.getBy(list{1, 4, 3, 2}, x => x > 3) /* Some(4) */
Belt.List.getBy(list{1, 4, 3, 2}, x => x > 4) /* None */
let getBy: (t<'a>, 'a => bool) => option<'a>
keepU
Uncurried version of keep.
let keepU: (t<'a>, (. 'a) => bool) => t<'a>
keep
Returns a list of all elements in someList
which satisfy the predicate function pred
.
RESlet isEven = x => mod(x, 2) == 0
Belt.List.keep(list{1, 2, 3, 4}, isEven) /* list{2, 4} */
Belt.List.keep(list{None, Some(2), Some(3), None}, Belt.Option.isSome) /* list{Some(2), Some(3)} */
let keep: (t<'a>, 'a => bool) => t<'a>
filter
Returns a list of all elements in someList
which satisfy the predicate function pred
.
RESlet isEven = x => mod(x, 2) == 0
Belt.List.filter(list{1, 2, 3, 4}, isEven) /* list{2, 4} */
Belt.List.filter(list{None, Some(2), Some(3), None}, Belt.Option.isSome) /* list{Some(2), Some(3)} */
let filter: (t<'a>, 'a => bool) => t<'a>
keepWithIndexU
Uncurried version of keepWithIndex.
let keepWithIndexU: (t<'a>, (. 'a, int) => bool) => t<'a>
keepWithIndex
Returns a list of all elements in someList
which satisfy the predicate function pred
.
RESlet isEven = x => mod(x, 2) == 0
Belt.List.keepWithIndex(list{1, 2, 3, 4}, (_x, index) => isEven(index)) /* list{1, 3} */
let keepWithIndex: (t<'a>, ('a, int) => bool) => t<'a>
filterWithIndex
Returns a list of all elements in someList
which satisfy the predicate function pred
.
RESlet isEven = x => mod(x, 2) == 0
Belt.List.filterWithIndex(list{1, 2, 3, 4}, (_x, index) => isEven(index)) /* list{1, 3} */
let filterWithIndex: (t<'a>, ('a, int) => bool) => t<'a>
keepMapU
Uncurried version of keepMap.
let keepMapU: (t<'a>, (. 'a) => option<'b>) => t<'b>
keepMap
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.
RESlet isEven = x => mod(x, 2) == 0
list{1, 2, 3, 4}
->Belt.List.keepMap(x =>
if (isEven(x)) {
Some(x)
} else {
None
}
) /* list{2, 4} */
list{Some(1), Some(2), None}->Belt.List.keepMap(x => x) /* list{1, 2} */
let keepMap: (t<'a>, 'a => option<'b>) => t<'b>
partitionU
Uncurried version of partition.
let partitionU: (t<'a>, (. 'a) => bool) => (t<'a>, t<'a>)
partition
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:
RES(elementsThatSatisfies, elementsThatDoesNotSatisfy)
RESBelt.List.partition(list{1, 2, 3, 4}, x => x > 2) /* (list{3, 4}, list{1, 2}) */
let partition: (t<'a>, 'a => bool) => (t<'a>, t<'a>)
unzip
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.
RESBelt.List.unzip(list{(1, 2), (3, 4)}) /* (list{1, 3}, list{2, 4}) */
Belt.List.unzip(list{("H", "W"), ("e", "o"), ("l", "r"), ("l", "l"), ("o", "d"), (" ", "!")})
/* (list{"H", "e", "l", "l", "o", " "}, list{"W", "o", "r", "l", "d", "!"}) */
let unzip: t<('a, 'b)> => (t<'a>, t<'b>)
getAssocU
Uncurried version of getAssoc.
let getAssocU: (t<('a, 'c)>, 'b, (. 'a, 'b) => bool) => option<'c>
getAssoc
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.
RESlist{(1, "a"), (2, "b"), (3, "c")}->Belt.List.getAssoc(3, (a, b) => a == b) /* Some("c") */
list{(9, "morning"), (15, "afternoon"), (22, "night")}
->Belt.List.getAssoc(15, (k, item) => k /* 15 */ == item /* 9, 5, 22 */)
/* Some("afternoon") */
let getAssoc: (t<('a, 'c)>, 'b, ('a, 'b) => bool) => option<'c>
hasAssocU
Uncurried version of hasAssoc.
let hasAssocU: (t<('a, 'c)>, 'b, (. 'a, 'b) => bool) => bool
hasAssoc
Returns true
if there is a pair in someList
where the first element equals k
as per the predicate function eqFunction
.
RESlist{(1, "a"), (2, "b"), (3, "c")}->Belt.List.hasAssoc(1, (a, b) => a == b) /* true */
list{(9, "morning"), (15, "afternoon"), (22, "night")}
->Belt.List.hasAssoc(25, (k, item) => k /* 25 */ == item /* 9, 5, 22 */) /* false */
let hasAssoc: (t<('a, 'c)>, 'b, ('a, 'b) => bool) => bool
removeAssocU
Uncurried version of removeAssoc.
let removeAssocU: (t<('a, 'c)>, 'b, (. 'a, 'b) => bool) => t<('a, 'c)>
removeAssoc
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
.
RESlist{(1, "a"), (2, "b"), (3, "c")}->Belt.List.removeAssoc(1, (a, b) => a == b) /* list{(2, "b"), (3, "c")} */
list{(9, "morning"), (15, "afternoon"), (22, "night")}
->Belt.List.removeAssoc(9, (k, item) => k /* 9 */ == item /* 9, 5, 22 */)
/* list{(15, "afternoon"), (22, "night")} */
let removeAssoc: (t<('a, 'c)>, 'b, ('a, 'b) => bool) => t<('a, 'c)>
setAssocU
Uncurried version of setAssoc.
let setAssocU: (t<('a, 'c)>, 'a, 'c, (. 'a, 'a) => bool) => t<('a, 'c)>
setAssoc
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
.
RESlist{(1, "a"), (2, "b"), (3, "c")}->Belt.List.setAssoc(2, "x", (a, b) => a == b) /* list{(1, "a"), (2, "x"), (3, "c")} */
list{(1, "a"), (3, "c")}->Belt.List.setAssoc(2, "b", (a, b) => a == b) /* list{(2, "b"), (1, "a"), (3, "c")} */
list{(9, "morning"), (3, "morning?!"), (22, "night")}
->Belt.List.setAssoc(15, "afternoon", (a, b) => mod(a, 12) == mod(b, 12))
/* list{(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.
let setAssoc: (t<('a, 'c)>, 'a, 'c, ('a, 'a) => bool) => t<('a, 'c)>
sortU
Uncurried version of sort.
let sortU: (t<'a>, (. 'a, 'a) => int) => t<'a>
sort
Returns a sorted list.
RESBelt.List.sort(list{5, 4, 9, 3, 7}, (a, b) => a - b) // list{3, 4, 5, 7, 9}
let sort: (t<'a>, ('a, 'a) => int) => t<'a>