List
Edit on GitHubUtilities for working with lists.
Added in 0.2.0
version | changes |
---|---|
0.1.0 | Originally named `lists` |
0.2.0 | Renamed to `list` |
Values
Functions and constants included in the List module.
List.init
Added in 0.3.0
No other changes yet.
Creates a new list of the specified length where each element is initialized with the result of an initializer function. The initializer is called with the index of each list element.
Parameters:
param | type | description |
---|---|---|
length |
Number |
The length of the new list |
fn |
Number => a |
The initializer function to call with each index, where the value returned will be used to initialize the element |
Returns:
type | description |
---|---|
List<a> |
The new list |
Examples:
List.length
Added in 0.1.0
version | changes |
---|---|
0.2.0 | Made the function tail-recursive |
Computes the length of the input list.
Parameters:
param | type | description |
---|---|---|
list |
List<a> |
The list to inspect |
Returns:
type | description |
---|---|
Number |
The number of elements in the list |
List.isEmpty
Added in 0.6.0
No other changes yet.
Determines if the list contains no elements.
Parameters:
param | type | description |
---|---|---|
list |
List<a> |
The list to inspect |
Returns:
type | description |
---|---|
Bool |
true if the list is empty and false otherwise |
List.reverse
Added in 0.1.0
No other changes yet.
Creates a new list with all elements in reverse order.
Parameters:
param | type | description |
---|---|---|
list |
List<a> |
The list to reverse |
Returns:
type | description |
---|---|
List<a> |
The new list |
List.append
Added in 0.1.0
No other changes yet.
Creates a new list with the elements of the first list followed by the elements of the second list.
Parameters:
param | type | description |
---|---|---|
list1 |
List<a> |
The list containing elements to appear first |
list2 |
List<a> |
The list containing elements to appear second |
Returns:
type | description |
---|---|
List<a> |
The new list containing elements from list1 followed by elements from list2 |
List.contains
Added in 0.1.0
No other changes yet.
Checks if the value is an element of the input list.
Uses the generic ==
structural equality operator.
Parameters:
param | type | description |
---|---|---|
search |
a |
The value to compare |
list |
List<a> |
The list to inspect |
Returns:
type | description |
---|---|
Bool |
true if the value exists in the list or false otherwise |
List.reduce
Added in 0.2.0
version | changes |
---|---|
0.1.0 | Originally named `foldLeft` |
0.2.0 | Renamed to `reduce` |
Combines all elements of a list using a reducer function, starting from the “head”, or left side, of the list.
In List.reduce(fn, initial, list)
, fn
is called with
an accumulator and each element of the list, and returns
a new accumulator. The final value is the last accumulator
returned. The accumulator starts with value initial
.
Parameters:
param | type | description |
---|---|---|
fn |
(a, b) => a |
The reducer function to call on each element, where the value returned will be the next accumulator value |
initial |
a |
The initial value to use for the accumulator on the first iteration |
list |
List<b> |
The list to iterate |
Returns:
type | description |
---|---|
a |
The final accumulator returned from fn |
Examples:
List.reduceRight
Added in 0.2.0
version | changes |
---|---|
0.1.0 | Originally named `foldRight` |
0.2.0 | Renamed to `reduceRight` |
Combines all elements of a list using a reducer function, starting from the “end”, or right side, of the list.
In List.reduceRight(fn, initial, list)
, fn
is called with
each element of the list and an accumulator, and returns
a new accumulator. The final value is the last accumulator
returned. The accumulator starts with value initial
.
Parameters:
param | type | description |
---|---|---|
fn |
(a, b) => b |
The reducer function to call on each element, where the value returned will be the next accumulator value |
initial |
b |
The initial value to use for the accumulator on the first iteration |
list |
List<a> |
The list to iterate |
Returns:
type | description |
---|---|
b |
The final accumulator returned from fn |
Examples:
List.map
Added in 0.1.0
No other changes yet.
Produces a new list initialized with the results of a mapper function called on each element of the input list.
Parameters:
param | type | description |
---|---|---|
fn |
a => b |
The mapper function to call on each element, where the value returned will be used to initialize the element in the new list |
list |
List<a> |
The list to iterate |
Returns:
type | description |
---|---|
List<b> |
The new list with mapped values |
List.mapi
Added in 0.1.0
No other changes yet.
Produces a new list initialized with the results of a mapper function called on each element of the input list and its index.
Parameters:
param | type | description |
---|---|---|
fn |
(a, Number) => b |
The mapper function to call on each element, where the value returned will be used to initialize the element in the new list |
list |
List<a> |
The list to iterate |
Returns:
type | description |
---|---|
List<b> |
The new list with mapped values |
List.flatMap
Added in 0.2.0
No other changes yet.
Produces a new list by calling a function on each element of the input list. Each iteration produces an intermediate list, which are all appended to produce a “flattened” list of all results.
Parameters:
param | type | description |
---|---|---|
fn |
a => List<b> |
The function to be called on each element, where the value returned will be a list that gets appended to the new list |
list |
List<a> |
The list to iterate |
Returns:
type | description |
---|---|
List<b> |
The new list |
List.every
Added in 0.1.0
No other changes yet.
Checks that the given condition is satisfied for all elements in the input list.
Parameters:
param | type | description |
---|---|---|
fn |
a => Bool |
The function to call on each element, where the returned value indicates if the element satisfies the condition |
list |
List<a> |
The list to check |
Returns:
type | description |
---|---|
Bool |
true if all elements satify the condition or false otherwise |
List.some
Added in 0.1.0
No other changes yet.
Checks that the given condition is satisfied at least once by an element in the input list.
Parameters:
param | type | description |
---|---|---|
fn |
a => Bool |
The function to call on each element, where the returned value indicates if the element satisfies the condition |
list |
List<a> |
The list to iterate |
Returns:
type | description |
---|---|
Bool |
true if one or more elements satify the condition or false otherwise |
List.forEach
Added in 0.1.0
No other changes yet.
Iterates a list, calling an iterator function on each element.
Parameters:
param | type | description |
---|---|---|
fn |
a => Void |
The iterator function to call with each element |
list |
List<a> |
The list to iterate |
List.forEachi
Added in 0.1.0
No other changes yet.
Iterates a list, calling an iterator function on each element. Also passes the index as the second argument to the function.
Parameters:
param | type | description |
---|---|---|
fn |
(a, Number) => Void |
The iterator function to call with each element |
list |
List<a> |
The list to iterate |
List.filter
Added in 0.1.0
No other changes yet.
Produces a new list by calling a function on each element of the input list and only including it in the result list if the element satisfies the condition.
Parameters:
param | type | description |
---|---|---|
fn |
a => Bool |
The function to call on each element, where the returned value indicates if the element satisfies the condition |
list |
List<a> |
The list to iterate |
Returns:
type | description |
---|---|
List<a> |
The new list containing elements where fn returned true |
List.filteri
Added in 0.3.0
No other changes yet.
Produces a new list by calling a function on each element of the input list and only including it in the result list if the element satisfies the condition. Also passes the index to the function.
Parameters:
param | type | description |
---|---|---|
fn |
(a, Number) => Bool |
The function to call on each element, where the returned value indicates if the element satisfies the condition |
list |
List<a> |
The list to iterate |
Returns:
type | description |
---|---|
List<a> |
The new list containing elements where fn returned true |
List.reject
Added in 0.1.0
No other changes yet.
Produces a new list by calling a function on each element of the input list and excluding it from the result list if the element satisfies the condition.
Parameters:
param | type | description |
---|---|---|
fn |
a => Bool |
The function to call on each element, where the returned value indicates if the element satisfies the condition |
list |
List<a> |
The list to iterate |
Returns:
type | description |
---|---|
List<a> |
The new list containing elements where fn returned false |
List.head
Added in 0.2.0
version | changes |
---|---|
0.1.0 | Originally named `hd` |
0.2.0 | Renamed to `head` |
0.3.0 | Return type converted to `Option` type |
Provides Some(element)
containing the first element, or “head”, of
the input list or None
if the list is empty.
Parameters:
param | type | description |
---|---|---|
list |
List<a> |
The list to access |
Returns:
type | description |
---|---|
Option<a> |
Some(firstElement) if the list has elements or None otherwise |
List.tail
Added in 0.2.0
version | changes |
---|---|
0.1.0 | Originally named `tl` |
0.2.0 | Renamed to `tail` |
0.3.0 | Return type converted to `Option` type |
Provides Some(tail)
containing all list items except the first element, or “tail”, of
the input list or None
if the list is empty.
Parameters:
param | type | description |
---|---|---|
list |
List<a> |
The list to access |
Returns:
type | description |
---|---|
Option<List<a>> |
Some(tail) if the list has elements or None otherwise |
List.nth
Added in 0.1.0
version | changes |
---|---|
0.1.0 | Originally failed for index out-of-bounds or list empty |
0.3.0 | Return type converted to `Option` type |
Provides Some(element)
containing the element in the list at the specified index
or None
if the index is out-of-bounds or the list is empty.
Parameters:
param | type | description |
---|---|---|
index |
Number |
The index to access |
list |
List<a> |
The list to access |
Returns:
type | description |
---|---|
Option<a> |
Some(element) if the list contains an element at the index or None otherwise |
List.flatten
Added in 0.1.0
No other changes yet.
Flattens nested lists.
Parameters:
param | type | description |
---|---|---|
list |
List<List<a>> |
The list to flatten |
Returns:
type | description |
---|---|
List<a> |
A new list containing all nested list elements combined |
Examples:
List.insert
Added in 0.1.0
version | changes |
---|---|
0.6.0 | Swapped order of `index` and `value` parameters |
Inserts a new value into a list at the specified index.
Parameters:
param | type | description |
---|---|---|
index |
Number |
The index to update |
value |
a |
The value to insert |
list |
List<a> |
The list to update |
Returns:
type | description |
---|---|
List<a> |
The new list |
Throws:
Failure(String)
- When
index
is negative - When
index
is more than 0 and greater than the list size
List.count
Added in 0.1.0
version | changes |
---|---|
0.2.0 | Made the function tail-recursive |
Counts the number of elements in a list that satisfy the given condition.
Parameters:
param | type | description |
---|---|---|
fn |
a => Bool |
The function to call on each element, where the returned value indicates if the element satisfies the condition |
list |
List<a> |
The list to iterate |
Returns:
type | description |
---|---|
Number |
The total number of elements that satisfy the condition |
List.part
Added in 0.1.0
No other changes yet.
Split a list into two, with the first list containing the required number of elements.
Parameters:
param | type | description |
---|---|---|
count |
Number |
The number of elements required |
list |
List<a> |
The list to split |
Returns:
type | description |
---|---|
(List<a>, List<a>) |
Two lists where the first contains exactly the required amount of elements and the second contains any remaining elements |
Throws:
Failure(String)
- When
count
is negative - When the list doesn’t contain at least the required amount of elements
List.rotate
Added in 0.1.0
version | changes |
---|---|
0.6.0 | No longer throws if `count` outside list length bounds |
Rotates list elements by the specified amount to the left, such that n
th
element is the first in the new list.
If value is negative, list elements will be rotated by the specified amount to the right. See examples.
Parameters:
param | type | description |
---|---|---|
n |
Number |
The number of elements to rotate by |
list |
List<a> |
The list to be rotated |
Examples:
List.unique
Added in 0.2.0
version | changes |
---|---|
0.1.0 | Originally named `uniq` |
0.2.0 | Renamed to `unique` |
Produces a new list with any duplicates removed.
Uses the generic ==
structural equality operator.
Parameters:
param | type | description |
---|---|---|
list |
List<a> |
The list to filter |
Returns:
type | description |
---|---|
List<a> |
The new list with only unique values |
List.zip
Added in 0.5.3
No other changes yet.
Produces a new list filled with tuples of elements from both given lists. The first tuple will contain the first item of each list, the second tuple will contain the second item of each list, and so on.
Calling this function with lists of different sizes will cause the returned list to have the length of the smaller list.
Parameters:
param | type | description |
---|---|---|
list1 |
List<a> |
The list to provide values for the first tuple element |
list2 |
List<b> |
The list to provide values for the second tuple element |
Returns:
type | description |
---|---|
List<(a, b)> |
The new list containing indexed pairs of (a, b) |
Examples:
List.zipWith
Added in 0.5.3
No other changes yet.
Produces a new list filled with elements defined by applying a function on pairs from both given lists. The first element will contain the result of applying the function to the first elements of each list, the second element will contain the result of applying the function to the second elements of each list, and so on.
Calling this function with lists of different sizes will cause the returned list to have the length of the smaller list.
Parameters:
param | type | description |
---|---|---|
fn |
(a, b) => c |
The function to apply to pairs of elements |
list1 |
List<a> |
The list whose elements will each be passed to the function as the first argument |
list2 |
List<b> |
The list whose elements will each be passed to the function as the second argument |
Returns:
type | description |
---|---|
List<c> |
The new list containing elements derived from applying the function to pairs of input list elements |
Examples:
List.unzip
Added in 0.5.3
No other changes yet.
Produces two lists by splitting apart a list of tuples.
Parameters:
param | type | description |
---|---|---|
list |
List<(a, b)> |
The list of tuples to split |
Returns:
type | description |
---|---|
(List<a>, List<b>) |
An list containing all elements from the first tuple element, and a list containing all elements from the second tuple element |
List.drop
Added in 0.2.0
No other changes yet.
Produces a new list with the specified number of elements removed from the beginning of the input list.
Parameters:
param | type | description |
---|---|---|
count |
Number |
The amount of elements to remove |
list |
List<a> |
The input list |
Returns:
type | description |
---|---|
List<a> |
The new list without the dropped elements |
Throws:
Failure(String)
- When
count
is negative
List.dropWhile
Added in 0.2.0
No other changes yet.
Produces a new list with the elements removed from the beginning
of the input list until they no longer satisfy the given condition.
Stops when the predicate function returns false
.
Parameters:
param | type | description |
---|---|---|
fn |
a => Bool |
The function to call on each element, where the returned value indicates if the element satisfies the condition |
list |
List<a> |
The input list |
Returns:
type | description |
---|---|
List<a> |
The new list without the dropped elements |
List.take
Added in 0.2.0
No other changes yet.
Produces a new list with–at most—the specified amount elements from the beginning of the input list.
Parameters:
param | type | description |
---|---|---|
count |
Number |
The amount of elements to keep |
list |
List<a> |
The input list |
Returns:
type | description |
---|---|
List<a> |
The new list containing the taken elements |
Throws:
Failure(String)
- When
count
is negative
List.takeWhile
Added in 0.2.0
No other changes yet.
Produces a new list with elements from the beginning of the input list
as long as they satisfy the given condition.
Stops when the predicate function returns false
.
Parameters:
param | type | description |
---|---|---|
fn |
a => Bool |
The function to call on each element, where the returned value indicates if the element satisfies the condition |
list |
List<a> |
The input list |
Returns:
type | description |
---|---|
List<a> |
The new list containing the taken elements |
List.find
Added in 0.2.0
version | changes |
---|---|
0.2.0 | Originally failed if the list was empty |
0.3.0 | Return type converted to `Option` type |
Finds the first element in a list that satifies the given condition.
Parameters:
param | type | description |
---|---|---|
fn |
a => Bool |
The function to call on each element, where the returned value indicates if the element satisfies the condition |
list |
List<a> |
The list to search |
Returns:
type | description |
---|---|
Option<a> |
Some(element) containing the first value found or None otherwise |
List.findIndex
Added in 0.2.0
version | changes |
---|---|
0.2.0 | Originally failed if the list was empty |
0.3.0 | Return type converted to `Option` type |
Finds the first index in a list where the element satifies the given condition.
Parameters:
param | type | description |
---|---|---|
fn |
a => Bool |
The function to call on each element, where the returned value indicates if the element satisfies the condition |
list |
List<a> |
The list to search |
Returns:
type | description |
---|---|
Option<Number> |
Some(index) containing the index of the first element found or None otherwise |
List.product
Added in 0.2.0
No other changes yet.
Combines two lists into a Cartesian product of tuples containing
all ordered pairs (a, b)
.
Parameters:
param | type | description |
---|---|---|
list1 |
List<a> |
The list to provide values for the first tuple element |
list2 |
List<b> |
The list to provide values for the second tuple element |
Returns:
type | description |
---|---|
List<(a, b)> |
The new list containing all pairs of (a, b) |
List.sub
Added in 0.2.0
No other changes yet.
Provides the subset of a list given zero-based start index and amount of elements to include.
Parameters:
param | type | description |
---|---|---|
start |
Number |
The index of the list where the subset will begin (inclusive) |
length |
Number |
The amount of elements to be included in the subset |
list |
List<a> |
The input list |
Returns:
type | description |
---|---|
List<a> |
The subset of the list |
Throws:
Failure(String)
- When
start
is negative - When
length
is negative
List.join
Added in 0.4.0
No other changes yet.
Combine the given list of strings into one string with the specified separator inserted between each item.
Parameters:
param | type | description |
---|---|---|
separator |
String |
The separator to insert between elements |
list |
List<String> |
The list to combine |
Returns:
type | description |
---|---|
String |
The combined elements with the separator between each |
List.revAppend
Added in 0.4.5
No other changes yet.
Reverses the first list and appends the second list to the end.
Parameters:
param | type | description |
---|---|---|
list1 |
List<a> |
The list to reverse |
list2 |
List<a> |
The list to append |
Returns:
type | description |
---|---|
List<a> |
The new list |
List.sort
Added in 0.4.5
version | changes |
---|---|
0.6.0 | Made `compare` a default argument |
Sorts the given list based on a given comparator function. The resulting list is sorted in increasing order.
Ordering is calculated using a comparator function which takes two list elements and must return 0 if both are equal, a positive number if the first is greater, and a negative number if the first is smaller.
Parameters:
param | type | description |
---|---|---|
?compare |
(num1: a, num2: a) => Number |
The comparator function used to indicate sort order |
list |
List<a> |
The list to be sorted |
Returns:
type | description |
---|---|
List<a> |
The sorted list |