List

add

RES
let add: (list<'a>, 'a) => list<'a>

add(list, value) adds a value to the beginning of list list.

Examples

RES
List.add(list{2, 3}, 1) == list{1, 2, 3} List.add(list{"World", "!"}, "Hello") == list{"Hello", "World", "!"}

compare

RES
let compare: (list<'a>, list<'a>, ('a, 'a) => Ordering.t) => Ordering.t

compare(list1, list2, f) compare elements one by one f. f returns a negative number if list1 is "less than" list2, zero if list1 is "equal to" list2, a positive number if list1 is "greater than" list2.

The comparison returns the first non-zero result of f, or zero if f returns zero for all list1 and list2.

  • If all items have compared equal, but list1 is exhausted first, return -1.. (list1 is shorter).

  • If all items have compared equal, but list2 is exhausted first, return 1. (list1 is longer).

Examples

RES
List.compare(list{3}, list{3, 7}, (a, b) => Int.compare(a, b)) == -1. List.compare(list{5, 3}, list{5}, (a, b) => Int.compare(a, b)) == 1. List.compare(list{1, 3, 5}, list{1, 4, 2}, (a, b) => Int.compare(a, b)) == -1. List.compare(list{1, 3, 5}, list{1, 2, 3}, (a, b) => Int.compare(a, b)) == 1. List.compare(list{1, 3, 5}, list{1, 3, 5}, (a, b) => Int.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.

compareLength

RES
let compareLength: (list<'a>, list<'a>) => Ordering.t

compareLength(list1, list2) compare two lists solely by length. Returns -1. if length(list1) is less than length(list2), 0. if length(list1) equals length(list2), and 1. if length(list1) is greater than length(list2).

Examples

RES
List.compareLength(list{1, 2}, list{3, 4, 5, 6}) == -1. List.compareLength(list{1, 2, 3}, list{4, 5, 6}) == 0. List.compareLength(list{1, 2, 3, 4}, list{5, 6}) == 1.

concat

RES
let concat: (list<'a>, list<'a>) => list<'a>

concat(list1, list2) returns the list obtained by adding list2 after list1.

Examples

RES
List.concat(list{1, 2, 3}, list{4, 5}) == list{1, 2, 3, 4, 5}

concatMany

RES
let concatMany: array<list<'a>> => list<'a>

concatMany(arr) returns the list obtained by concatenating all the lists in array arr, in order.

Examples

RES
List.concatMany([list{1, 2, 3}, list{}, list{3}]) == list{1, 2, 3, 3}

drop

RES
let drop: (list<'a>, int) => option<list<'a>>

drop(list, value) return a new list, dropping the first value element. Returns None if list has fewer than value elements.

Examples

RES
list{1, 2, 3}->List.drop(2) == Some(list{3}) list{1, 2, 3}->List.drop(3) == Some(list{}) list{1, 2, 3}->List.drop(4) == None

equal

RES
let equal: (list<'a>, list<'a>, ('a, 'a) => bool) => bool

equal(list1, list2, f) check equality of list2 and list2 using f for equality on elements, where f is a function that returns true if items x and y meet some criterion for equality, false otherwise. equal false if length of list1 and list2 are not the same.

Examples

RES
List.equal(list{1, 2, 3}, list{1, 2}, (a, b) => a == b) == false List.equal(list{1, 2}, list{1, 2}, (a, b) => a == b) == true List.equal(list{1, 2, 3}, list{-1, -2, -3}, (a, b) => abs(a) == abs(b)) == true

every

RES
let every: (list<'a>, 'a => bool) => bool

every(list, f) returns true if all elements in list satisfy f, where f is a predicate: a function taking an element and returning a bool.

Examples

RES
let isBelow10 = value => value < 10 list{1, 9, 8, 2}->List.every(isBelow10) == true list{1, 99, 8, 2}->List.every(isBelow10) == false

every2

RES
let every2: (list<'a>, list<'b>, ('a, 'b) => bool) => bool

every2(list1, list2, f) returns true if predicate f is true for all pairs of elements up to the shorter length (i.e. min(length(list1), length(list2)))

Examples

RES
List.every2(list{1, 2, 3}, list{0, 1}, (a, b) => a > b) == true List.every2(list{}, list{1}, (a, b) => a > b) == true List.every2(list{2, 3}, list{1}, (a, b) => a > b) == true List.every2(list{0, 1}, list{5, 0}, (a, b) => a > b) == false

filter

RES
let filter: (list<'a>, 'a => bool) => list<'a>

filter(list, f) returns a list of all elements in list which satisfy the predicate function f.

Examples

RES
let isEven = x => mod(x, 2) == 0 List.filter(list{1, 2, 3, 4}, isEven) == list{2, 4} List.filter(list{None, Some(2), Some(3), None}, Option.isSome) == list{Some(2), Some(3)}

filterMap

RES
let filterMap: (list<'a>, 'a => option<'b>) => list<'b>

filterMap(list, f) applies f to each element of list. If f returns Some(value), then value is kept in the resulting list. If f returns None, the element is not retained in the result.

Examples

RES
let isEven = x => mod(x, 2) == 0 list{1, 2, 3, 4}->List.filterMap(x => if isEven(x) { Some(x) } else { None } ) // list{2, 4} list{Some(1), Some(2), None}->List.filterMap(x => x) == list{1, 2}

filterWithIndex

RES
let filterWithIndex: (list<'a>, ('a, int) => bool) => list<'a>

filterWithIndex(list, f) returns a list of all elements in list which satisfy the predicate function f.

Examples

RES
let isEven = x => mod(x, 2) == 0 List.filterWithIndex(list{1, 2, 3, 4}, (_x, index) => isEven(index)) == list{1, 3}

find

RES
let find: (list<'a>, 'a => bool) => option<'a>

find(list, f) returns Some(value) for the first value in list that satisfies the predicate function f. Returns None if no element satisfies the function.

Examples

RES
List.find(list{1, 4, 3, 2}, x => x > 3) == Some(4) List.find(list{1, 4, 3, 2}, x => x > 4) == None

flat

RES
let flat: list<list<'a>> => list<'a>

flat(list) return the list obtained by concatenating all the lists in list, in order.

Examples

RES
List.flat(list{list{1, 2, 3}, list{}, list{3}}) == list{1, 2, 3, 3}

forEach

RES
let forEach: (list<'a>, 'a => unit) => unit

forEach(list, f) call f on each element of list 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.

Examples

RES
List.forEach(list{"a", "b", "c"}, x => Console.log("Item: " ++ x)) /* prints: Item: a Item: b Item: c */

forEach2

RES
let forEach2: (list<'a>, list<'b>, ('a, 'b) => 'c) => unit

forEach2(list1, list2, f) is similar to forEach, but accepts two lists and stops at the length of the shorter list.

Examples

RES
List.forEach2(list{"Z", "Y"}, list{"A", "B", "C"}, (x, y) => Console.log2(x, y)) /* prints: "Z" "A" "Y" "B" */

forEachWithIndex

RES
let forEachWithIndex: (list<'a>, ('a, int) => unit) => unit

forEachWithIndex(list, f, index) call f on each element of list from beginning to end. Function f takes two arguments: the index starting from 0 and the element from list. f returns unit.

Examples

RES
List.forEachWithIndex(list{"a", "b", "c"}, (x, index) => { Console.log("Item " ++ Int.toString(index) ++ " is " ++ x) }) /* prints: Item 0 is a Item 1 is b Item 2 is cc */

fromArray

RES
let fromArray: array<'a> => list<'a>

fromArray(arr) converts the given array arr to a list.

Examples

RES
List.fromArray([1, 2, 3]) == list{1, 2, 3}

fromInitializer

RES
let fromInitializer: (~length: int, int => 'a) => list<'a>

fromInitializer(length, f) return a list of length length with element initialized with f. Returns an empty list if length is negative.

Examples

RES
List.fromInitializer(~length=5, i => i) == list{0, 1, 2, 3, 4} List.fromInitializer(~length=5, i => i * i) == list{0, 1, 4, 9, 16}

get

RES
let get: (list<'a>, int) => option<'a>

get(list, index) return the index element in list, or None if index is larger than the length of list list.

Examples

RES
let abc = list{"A", "B", "C"} abc->List.get(1) == Some("B") abc->List.get(4) == None

getAssoc

Deprecated

Use a Map instead

RES
let getAssoc: (list<('a, 'c)>, 'b, ('a, 'b) => bool) => option<'c>

getAssoc(list, k, f) return the second element of a pair in list where the first element equals k as per the predicate function f, or None if not found.

Examples

RES
list{(1, "a"), (2, "b"), (3, "c")}->List.getAssoc(3, (a, b) => a == b) // Some("c") list{(9, "morning"), (15, "afternoon"), (22, "night")}->List.getAssoc(15, (k, item) => k /* 15 */ == item ) == Some("afternoon") /* 9, 5, 22 */

getExn

Deprecated

RES
let getExn: (list<'a>, int) => 'a

getExn(list, index) same as get.

Examples

RES
let abc = list{"A", "B", "C"} abc->List.getExn(1) == "B" switch abc->List.getExn(4) { | exception Not_found => assert(true) | _ => assert(false) }

Exceptions

  • Throws an Error if index is larger than the length of list.

getOrThrow

RES
let getOrThrow: (list<'a>, int) => 'a

getOrThrow(list, index) same as get.

Examples

RES
let abc = list{"A", "B", "C"} abc->List.getOrThrow(1) == "B" switch abc->List.getOrThrow(4) { | exception Not_found => assert(true) | _ => assert(false) }

Exceptions

  • Throws an Error if index is larger than the length of list.

has

RES
let has: (list<'a>, 'b, ('a, 'b) => bool) => bool

has(list, element, f) returns true if the list contains at least one element for which f returns `true'.

Examples

RES
list{1, 2, 3}->List.has(2, (a, b) => a == b) == true list{1, 2, 3}->List.has(4, (a, b) => a == b) == false list{-1, -2, -3}->List.has(2, (a, b) => abs(a) == abs(b)) == true

hasAssoc

Deprecated

Use a Map instead

RES
let hasAssoc: (list<('a, 'c)>, 'b, ('a, 'b) => bool) => bool

hasAssoc(list, k, f) returns true if there is a pair in list where the first element equals k as per the predicate function f.

Examples

RES
list{(1, "a"), (2, "b"), (3, "c")}->List.hasAssoc(1, (a, b) => a == b) // true list{(9, "morning"), (15, "afternoon"), (22, "night")}->List.hasAssoc(25, (k, item) => k /* 25 */ == item ) == false /* 9, 5, 22 */

head

RES
let head: list<'a> => option<'a>

head(list) returns Some(value) where value is the first element in the list, or None if list is an empty list.

Examples

RES
List.head(list{}) == None List.head(list{1, 2, 3}) == Some(1)

headExn

Deprecated

RES
let headExn: list<'a> => 'a

headExn(list) same as head.

Examples

RES
List.headExn(list{1, 2, 3}) == 1 switch List.headExn(list{}) { | exception Not_found => assert(true) | _ => assert(false) }

Exceptions

  • Throws an Error if list is empty.

headOrThrow

RES
let headOrThrow: list<'a> => 'a

headOrThrow(list) same as head.

Examples

RES
List.headOrThrow(list{1, 2, 3}) == 1 switch List.headOrThrow(list{}) { | exception Not_found => assert(true) | _ => assert(false) }

Exceptions

  • Throws an Error if list is empty.

ignore

RES
let ignore: list<'a> => unit

ignore(list) ignores the provided list and returns unit.

This helper is useful when you want to discard a value (for example, the result of an operation with side effects) without having to store or process it further.

length

RES
let length: list<'a> => int

length(list) returns the length of list.

Examples

RES
List.length(list{1, 2, 3}) == 3

make

RES
let make: (~length: int, 'a) => list<'a>

make(length, value) returns a list of length length with each element filled with value. Returns an empty list if value is negative.

Examples

RES
List.make(~length=3, 1) == list{1, 1, 1}

map

RES
let map: (list<'a>, 'a => 'b) => list<'b>

map(list, f) returns a new list with f applied to each element of list.

Examples

RES
list{1, 2}->List.map(x => x + 1) == list{2, 3}

mapReverse

RES
let mapReverse: (list<'a>, 'a => 'b) => list<'b>

mapReverse(list, f) is equivalent to map function.

Examples

RES
let f = x => x * x let l = list{3, 4, 5} let withMap = List.map(l, f)->List.reverse let withMapReverse = l->List.mapReverse(f) Console.log(withMap == withMapReverse) // true

mapReverse2

RES
let mapReverse2: (list<'a>, list<'b>, ('a, 'b) => 'c) => list<'c>

mapReverse2(list1, list2, f) is equivalent to List.zipBy(list1, list2, f)->List.reverse.

Examples

RES
List.mapReverse2(list{1, 2, 3}, list{1, 2}, (a, b) => a + b) == list{4, 2}

mapWithIndex

RES
let mapWithIndex: (list<'a>, ('a, int) => 'b) => list<'b>

mapWithIndex(list, f) applies f to each element of list. Function f takes two arguments: the index starting from 0 and the element from list, in that order.

Examples

RES
list{1, 2, 3}->List.mapWithIndex((x, index) => index + x) == list{1, 3, 5}

partition

RES
let partition: (list<'a>, 'a => bool) => (list<'a>, list<'a>)

partition(list, f) creates a pair of lists; the first list consists of all elements of list that satisfy the predicate function f, the second list consists of all elements of list that do not satisfy f.

Examples

RES
// (elementsThatSatisfies, elementsThatDoesNotSatisfy) List.partition(list{1, 2, 3, 4}, x => x > 2) == (list{3, 4}, list{1, 2})

reduce

RES
let reduce: (list<'a>, 'b, ('b, 'a) => 'b) => 'b

reduce(list, initialValue, f) applies f to each element of list 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.

Examples

RES
list{1, 2, 3, 4}->List.reduce(0, (a, b) => a + b) == 10 // same as list{1, 2, 3, 4}->List.reduce(0, (acc, item) => acc + item) == 10

reduce2

RES
let reduce2: (list<'b>, list<'c>, 'a, ('a, 'b, 'c) => 'a) => 'a

reduce2(list1, list2, initialValue, f) applies f to each element of list1 and list2 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 l1, and an item from l2. reduce2 returns the final value of the accumulator.

Examples

RES
List.reduce2(list{1, 2, 3}, list{4, 5}, 0, (acc, x, y) => acc + x * x + y) == 0 + (1 * 1 + 4) + (2 * 2 + 5)

reduceReverse

RES
let reduceReverse: (list<'a>, 'b, ('b, 'a) => 'b) => 'b

reduceReverse(list, initialValue, f) works like reduce, except that function f is applied to each item of list from the last back to the first.

Examples

RES
list{1, 2, 3, 4}->List.reduceReverse(0, (a, b) => a + b) == 10 list{1, 2, 3, 4}->List.reduceReverse(10, (a, b) => a - b) == 0 list{1, 2, 3, 4}->List.reduceReverse(list{}, List.add) == list{1, 2, 3, 4}

reduceReverse2

RES
let reduceReverse2: (list<'a>, list<'b>, 'c, ('c, 'a, 'b) => 'c) => 'c

reduceReverse2(list1, list2, initialValue, f) applies f to each element of list1 and list2from end to beginning. Stops with the shorter list. Function f has three parameters: an accumulator which starts with a value of initialValue, an item from l1, and an item from l2. reduce2 returns the final value of the accumulator.

Examples

RES
List.reduceReverse2(list{1, 2, 3}, list{4, 5}, 0, (acc, x, y) => acc + x * x + y) == 0 + (1 * 1 + 4) + (2 * 2 + 5)

reduceWithIndex

RES
let reduceWithIndex: (list<'a>, 'b, ('b, 'a, int) => 'b) => 'b

reduceWithIndex(list, initialValue, f) applies f to each element of list 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.

Examples

RES
list{1, 2, 3, 4}->List.reduceWithIndex(0, (acc, item, index) => acc + item + index) == 16

removeAssoc

Deprecated

Use a Map instead

RES
let removeAssoc: (list<('a, 'c)>, 'b, ('a, 'b) => bool) => list<('a, 'c)>

removeAssoc(list, k, f) return a list after removing the first pair whose first value is k per the equality predicate f, if not found, return a new list identical to list.

Examples

RES
list{(1, "a"), (2, "b"), (3, "c")}->List.removeAssoc(1, (a, b) => a == b) // list{(2, "b"), (3, "c")} list{(9, "morning"), (15, "afternoon"), (22, "night")}->List.removeAssoc(9, (k, item) => k /* 9 */ == item ) == list{(15, "afternoon"), (22, "night")} /* 9, 5, 22 */

reverse

RES
let reverse: list<'a> => list<'a>

reverse(list) returns a new list whose elements are those of list in reversed order.

Examples

RES
List.reverse(list{1, 2, 3}) == list{3, 2, 1}

reverseConcat

RES
let reverseConcat: (list<'a>, list<'a>) => list<'a>

reverseConcat(list1, list2) is equivalent to writing: concat(reverse(list1, list2)

Examples

RES
List.reverseConcat(list{1, 2}, list{3, 4}) == list{2, 1, 3, 4}

setAssoc

Deprecated

Use a Map instead

RES
let setAssoc: (list<('a, 'c)>, 'a, 'c, ('a, 'a) => bool) => list<('a, 'c)>

setAssoc(list, k, v, f). If k exists in list by satisfying the f 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 list.

Examples

RES
list{(1, "a"), (2, "b"), (3, "c")}->List.setAssoc(2, "x", (a, b) => a == b) == list{(1, "a"), (2, "x"), (3, "c")} list{(1, "a"), (3, "c")}->List.setAssoc(2, "b", (a, b) => a == b) == list{(2, "b"), (1, "a"), (3, "c")} list{(9, "morning"), (3, "morning?!"), (22, "night")}->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.

shuffle

RES
let shuffle: list<'a> => list<'a>

shuffle(list) returns a new list in random order.

Examples

RES
List.shuffle(list{1, 2, 3}) // list{2, 1, 3}

size

RES
let size: list<'a> => int

size(list). See length

Examples

RES
List.size(list{1, 2, 3}) == 3

some

RES
let some: (list<'a>, 'a => bool) => bool

some(list, f) returns true if at least one of the elements in list satisfies f, where f is a predicate: a function taking an element and returning a bool.

Examples

RES
let isAbove100 = value => value > 100 list{101, 1, 2, 3}->List.some(isAbove100) == true list{1, 2, 3, 4}->List.some(isAbove100) == false

some2

RES
let some2: (list<'a>, list<'b>, ('a, 'b) => bool) => bool

some2(list1, list2, f) returns true if predicate f is true for any pair of elements up to the shorter length (i.e. min(length(list1), length(list2)))

Examples

RES
List.some2(list{1, 2, 3}, list{0, 1}, (a, b) => a > b) == true List.some2(list{}, list{1}, (a, b) => a > b) == false List.some2(list{2, 3}, list{1}, (a, b) => a > b) == true List.some2(list{0, 1}, list{5, 0}, (a, b) => a > b) == true

sort

RES
let sort: (list<'a>, ('a, 'a) => Ordering.t) => list<'a>

sort(list, f) returns a sorted list.

Examples

RES
List.sort(list{5, 4, 9, 3, 7}, Int.compare) == list{3, 4, 5, 7, 9}

splitAt

RES
let splitAt: (list<'a>, int) => option<(list<'a>, list<'a>)>

splitAt(list, n) split the list list at n. Returns None when the length of list is less than n.

Examples

RES
list{"Hello", "World"}->List.splitAt(1) == Some((list{"Hello"}, list{"World"})) list{0, 1, 2, 3, 4}->List.splitAt(2) == Some((list{0, 1}, list{2, 3, 4}))

t

RES
type t<'a> = list<'a>

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.

tail

RES
let tail: list<'a> => option<list<'a>>

tail(list) returns None if list is empty, otherwise it returns Some(tail) where tail is everything except the first element of list.

Examples

RES
List.tail(list{1, 2, 3}) == Some(list{2, 3}) List.tail(list{}) == None

tailExn

Deprecated

RES
let tailExn: list<'a> => list<'a>

tailExn(list) same as tail.

Examples

RES
List.tailExn(list{1, 2, 3}) == list{2, 3} switch List.tailExn(list{}) { | exception Not_found => assert(true) | _ => assert(false) }

Exceptions

  • Throws an Error if list is empty.

tailOrThrow

RES
let tailOrThrow: list<'a> => list<'a>

tailOrThrow(list) same as tail.

Examples

RES
List.tailOrThrow(list{1, 2, 3}) == list{2, 3} switch List.tailOrThrow(list{}) { | exception Not_found => assert(true) | _ => assert(false) }

Exceptions

  • Throws an Error if list is empty.

take

RES
let take: (list<'a>, int) => option<list<'a>>

take(list, value) returns a list with the first value elements from list, or None if list has fewer than value elements.

Examples

RES
list{1, 2, 3}->List.take(1) == Some(list{1}) list{1, 2, 3}->List.take(2) == Some(list{1, 2}) list{1, 2, 3}->List.take(4) == None

toArray

RES
let toArray: list<'a> => array<'a>

toArray(list) converts the given list list to an array.

Examples

RES
List.toArray(list{1, 2, 3}) == [1, 2, 3]

toShuffled

Deprecated

RES
let toShuffled: list<'a> => list<'a>

toShuffled(list) returns a new list in random order.

Examples

RES
List.toShuffled(list{1, 2, 3}) // list{2, 1, 3}

unzip

RES
let unzip: list<('a, 'b)> => (list<'a>, list<'b>)

unzip(list) 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.

Examples

RES
List.unzip(list{(1, 2), (3, 4)}) // (list{1, 3}, list{2, 4}) 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", "!"})

zip

RES
let zip: (list<'a>, list<'b>) => list<('a, 'b)>

zip(list1, list2) returns a list of pairs from the two lists with the length of the shorter list.

Examples

RES
List.zip(list{1, 2}, list{3, 4, 5}) == list{(1, 3), (2, 4)}

zipBy

RES
let zipBy: (list<'a>, list<'b>, ('a, 'b) => 'c) => list<'c>

zipBy(list1, list2, f). See zip

Examples

RES
List.zipBy(list{1, 2, 3}, list{4, 5}, (a, b) => 2 * a + b) == list{6, 9}