List

This module contains type-safe functions for working with arrays.
How to import
import { List } from 'purify-ts/List'

Exports

at
Int -> [a] -> Maybe a<T>(index: number, list: T[]): Maybe<T>
Returns the element at a given index of a list.
List.at(0, [1, 2])
List.at(2, [1, 2])
Just(1)
Nothing
head
[a] -> Maybe a<T>(list: T[]): Maybe<T>
Returns Just the first element of an array or Nothing if there is none. If you don't want to work with a Maybe but still keep type safety, check out `NonEmptyList`.
List.head([1])
List.head([])
Just(1)
Nothing
last
[a] -> Maybe a<T>(list: T[]): Maybe<T>
Returns Just the last element of an array or Nothing if there is none.
List.last([1, 2, 3])
List.last([])
Just(3)
Nothing
tail
[a] -> Maybe [a]<T>(list: T[]): Maybe<T[]>
Returns all elements of an array except the first.
List.tail([1, 2, 3])
List.tail([1])
List.tail([])
Just([2, 3])
Just([])
Nothing
init
[a] -> Maybe [a] <T>(list: T[]): Maybe<T[]>
Returns all elements of an array except the last.
List.init([1, 2, 3])
List.init([1])
List.init([])
Just([1, 2])
Just([])
Nothing
find
(a -> Int -> [a] -> Bool) -> [a] -> Maybe a<T>(f: (x: T, index: number, arr: T[]) => boolean, list: T[]): Maybe<T>
Returns the first element which satisfies a predicate. A more typesafe version of the already existing List.prototype.find.
List.find(x => x > 5, [1,3,7,9])
List.find(x => x > 5)([1,3,7,9])
List.find(x => x > 10, [1,3,7,9])
Just(7)
Just(7)
Nothing
findIndex
(a -> Int -> [a] -> Bool) -> [a] -> Maybe Int<T>(f: (x: T, index: number, arr: T[]) => boolean, list: T[]): Maybe<number>
Returns the index of the first element which satisfies a predicate. A more typesafe version of the already existing List.prototype.findIndex.
List.findIndex(x => x > 5, [1,3,7,9])
List.findIndex(x => x > 5)([1,3,7,9])
List.findIndex(x => x > 10, [1,3,7,9])
Just(2)
Just(2)
Nothing
uncons
[a] -> Maybe (a, [a]) <T>(list: T[]): Maybe<Tuple<T, T[]>>
Returns a tuple of an array's head and tail.
List.uncons([1, 2, 3])
List.uncons([1])
List.uncons([])
Just(Tuple(1, [2, 3]))
Just(Tuple(1, []))
Nothing
sum
[Int] -> Int(list: number[]): number
Returns the sum of all numbers inside an array.
List.sum([])
List.sum([1, 2, 3])
0
6
sort
(a -> a -> Order) -> [a] -> [a]<T>(compare: (a: T, b: T) => Order, list: T[]): T[]
Sorts an array with the given comparison function.
import { compare } from 'purify-ts/Function'
List.sort(compare, [1,100,-1])
import { Order } from 'purify-ts/Function'
List.sort((x, y) => /* your own fn */, [0,102,-223])
[-1, 1, 100]
// Result depends on the returned Order enum value (Order.LT, Order.EQ or Order.GT)