List

GitHub   Edit on GitHub

Utilities for working with lists.

1
import List from "list"

Values

List.init

1
init : (Number, Number -> a) -> List<a>

List.init(n, fn) creates a new list of length n where each value is initialized with the result of the initializer fn. The initializer is called with the index of each list element.

1
List.init(5, n => n + 3) // [8, 9, 10, 11, 12]

List.length

1
length : List<a> -> Number

Computes the length of the input list.

List.sum

1
sum : List<Number> -> Number

Adds all numbers in the input list.

List.reverse

1
reverse : List<a> -> List<a>

Reverses the input list.

List.append

1
append : (List<a>, List<a>) -> List<a>

List.append(list1, list2) creates a new list with the elements of list1 followed by the elements of list2.

List.contains

1
contains : (a, List<a>) -> Bool

Checks if the item is an element of the input list. Uses the generic == equality operator.

List.reduce

1
reduce : ((b, a) -> b, b, List<a>) -> b

Using a reducer function, reduce combines all elements of a list, starting from the “head”, or left side, of the list.

In List.reduce(fn, base, list), fn is called with the accumulator and each element of the list, and returns a new accumulator. The final value is the result of reduce.
The accumulator starts with value base.

1
2
3
let add = a + b
let sum = (list) => List.reduce(add, 0, list)
sum([1, 2, 3]) // 6

List.reduceRight

1
reduceRight : ((a, b) -> b, b, List<a>) -> b

Using a reducer function, reduceRight combines all elements of a list, starting from the end, or right side, of the list.

In List.reduceRight(fn, base, list), fn is called with the each element of the list and the accumulator, and returns a new accumulator. The final value is the result of reduceRight.
The accumulator starts with value base.

1
2
3
let add = a + b
let sum = (list) => List.reduceRight(add, 0, list)
sum([1, 2, 3]) // 6

List.map

1
map : (a -> b, List<a>) -> List<b>

List.map(fn, list) produces a new list by calling fn on each element of the list.

List.mapi

1
mapi : ((a, Number) -> b, List<a>) -> List<b>

List.mapi(fn, list) produces a new list by calling fn on each element of the input list, along with the index of the element.

List.flatMap

1
flatMap : ((a) -> List<b>, List<a>) -> List<b>

List.flatMap(fn, list) produces a new list by calling fn on each element of the input list. Each iteration produces an intermediate list, which are all appended to produce a “flattened” list of all values.

List.every

1
every : (a -> Bool, List<a>) -> Bool

Checks that the given condition is satisfied for all items in the input list.

List.some

1
some : (a -> Bool, List<a>) -> Bool

Checks that the given condition is satisfied at least once by an item in the input list.

List.forEach

1
forEach : (a -> Void, List<a>) -> Void

Evaluates the given function for each item in the list.

List.forEachi

1
forEachi : ((a, Number) -> Void, List<a>) -> Void

Evaluates the given function for each item in the list. The given function is called with the element and its index in the list.

List.filter

1
filter : (a -> Bool, List<a>) -> List<a>

List.filter(fn, list) produces a new list by calling fn on each element of the list. If the fn returns false, the value will not be included the new list.

List.reject

1
reject : (a -> Bool, List<a>) -> List<a>

List.reject(fn, list) produces a new list by calling fn on each element of the list. If the fn returns true, the value will not be included in the new list.

List.head

1
head : List<a> -> Option<a>

Returns Some(element) containing the first element from the list or None if the list is empty.

List.tail

1
tail : List<a> -> Option<List<a>>

Returns Some(elements) containing the all elements in a list except the first elementt, Some([]) if the list only contains one element, or None if the list is empty.

List.nth

1
nth : (Number, List<a>) -> Option<a>

Returns Some(element) containing the element in the list at the index provided or None if the index is out-of-bounds or if the list is empty.

List.flatten

1
flatten : List<List<a>> -> List<a>

Turns a list of lists of values into a “flattened” list of the values.

List.insert

1
insert : (a, Number, List<a>) -> List<a>

Attempts to insert a value into the input list at the given index. Fails if the index is out-of-bounds.

List.count

1
count : (a -> Bool, List<a>) -> Number

Counts the number of elements in a list that satisfy the given condition.

List.part

1
part : (Number, List<a>) -> (List<a>, List<a>)

Splits a list into two lists: a list containing the first count elements, and a list containing the remaining elements. Fails if the count is out-of-bounds.

List.rotate

1
rotate : (Number, List<a>) -> List<a>

Produces a new list where the count elements are moved to the end of the list. If a negative count is provided, moves the last -count elements to the beginning of the list.

List.unique

1
unique : List<a> -> List<a>

Produces a new list with any duplicates removed. Uses the generic == structural equality operator.

List.drop

1
drop : (Number, List<a>) -> List<a>

Removes the first n items from the list. Fails if n is a negative number.

List.dropWhile

1
dropWhile : (a -> Bool, List<a>) -> List<a>

Removes items from the beginning of the list until the given function returns false.

List.take

1
take : (Number, List<a>) -> List<a>

Returns the first n items from the list. Fails if n is a negative number.

List.takeWhile

1
takeWhile : (a -> Bool, List<a>) -> List<a>

Returns a list containing the first elements satisfying the given predicate. Stops when the function returns false.

List.find

1
find : (a -> Bool, List<a>) -> Option<a>

List.find(fn, list) calls fn on each element of the list and returns Some(element) containing the first element for which fn returns true or None if no element was found.

List.findIndex

1
findIndex : (a -> Bool, List<a>) -> Option<Number>

List.findIndex(fn, list) calls fn on each element of the list and returns Some(index) containing the index of the first element for which fn returns true or None if no element was found.

List.product

1
product : (List<a>, List<b>) -> List<(a, b)>

Combines two lists into a Cartesian product of tuples containing all ordered pairs (a, b).

List.sub

1
sub : (Number, Number, List<a>) -> List<a>

Returns a sub-list of the given list starting from index start (inclusive) and up to length elements. Fails if start or length are negative numbers.

This is a notification!