List
Edit on GitHubUtilities for working with lists.
Values
List.init
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.
List.length
Computes the length of the input list.
List.sum
Adds all numbers in the input list.
List.reverse
Reverses the input list.
List.append
List.append(list1, list2)
creates a new list with the elements of list1
followed by the elements of list2
.
List.contains
Checks if the item is an element of the input list. Uses the generic ==
equality operator.
List.reduce
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
.
List.reduceRight
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
.
List.map
List.map(fn, list)
produces a new list by calling fn
on each element of the list.
List.mapi
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
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
Checks that the given condition is satisfied for all items in the input list.
List.some
Checks that the given condition is satisfied at least once by an item in the input list.
List.forEach
Evaluates the given function for each item in the list.
List.forEachi
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
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
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
Returns Some(element)
containing the first element from the list or None
if the list is empty.
List.tail
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
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
Turns a list of lists of values into a “flattened” list of the values.
List.insert
Attempts to insert a value into the input list at the given index. Fails if the index is out-of-bounds.
List.count
Counts the number of elements in a list that satisfy the given condition.
List.part
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
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
Produces a new list with any duplicates removed. Uses the generic ==
structural equality operator.
List.drop
Removes the first n
items from the list. Fails if n
is a negative number.
List.dropWhile
Removes items from the beginning of the list until the given function returns false
.
List.take
Returns the first n
items from the list. Fails if n
is a negative number.
List.takeWhile
Returns a list containing the first elements satisfying the given predicate. Stops when the function returns false
.
List.find
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
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
Combines two lists into a Cartesian product of tuples containing all ordered pairs (a, b)
.
List.sub
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.