SagaSaga
Standard Library

Std.List

Operations on linked lists: transforming, searching, sorting, and more.

Functions

fun head : List a -> a

Returns the first element of a list. Panics on empty list.

tail

fun tail : List a -> List a

Returns all elements after the first. Panics on empty list.

last

fun last : List a -> a

Returns the last element of a list. Panics on empty list.

length

fun length : (xs: List a) -> Int

Returns the number of elements in a list.

is_empty

fun is_empty : List a -> Bool

Returns True if the list has no elements.

map

fun map : (f: a -> b needs {..e}) -> (xs: List a) -> List b needs {..e}

Applies a function to every element, returning a new list of results.

filter

fun filter : (pred: a -> Bool needs {..e}) -> (xs: List a) -> List a needs {..e}

Returns a list of elements for which the predicate returns True.

foldl

fun foldl : (f: b -> a -> b needs {..e}) -> (acc: b) -> (xs: List a) -> b needs {..e}

Left fold. Applies f to the accumulator and each element, left to right.

foldr

fun foldr : (f: a -> b -> b needs {..e}) -> (acc: b) -> (xs: List a) -> b needs {..e}

Right fold. Applies f to each element and the accumulator, right to left.

reverse

fun reverse : (xs: List a) -> List a

Returns a list with elements in reverse order.

append

fun append : (xs: List a) -> (ys: List a) -> List a

Concatenates two lists.

flat_map

fun flat_map : (f: a -> List b needs {..e}) -> (xs: List a) -> List b needs {..e}

Maps a function over a list and flattens the results by one level.

filter_map

fun filter_map : (f: a -> Maybe b needs {..e}) -> (xs: List a) -> List b needs {..e}

Maps a function over a list and keeps only the Just results.

take

fun take : (n: Int) -> (xs: List a) -> List a

Returns the first n elements of a list.

drop

fun drop : (n: Int) -> (xs: List a) -> List a

Drops the first n elements of a list and returns the rest.

zip

fun zip : (xs: List a) -> (ys: List b) -> List (a, b)

Pairs up elements from two lists. Stops at the shorter list.

any

fun any : (pred: a -> Bool needs {..e}) -> (xs: List a) -> Bool needs {..e}

Returns True if any element satisfies the predicate.

all

fun all : (pred: a -> Bool needs {..e}) -> (xs: List a) -> Bool needs {..e}

Returns True if all elements satisfy the predicate.

range

fun range : (start: Int) -> (end: Int) -> List Int

Creates a list of integers from start to end, inclusive.

sort

fun sort : (xs: List a) -> List a where {a: Ord}

Sorts a list in ascending order using the Ord instance.

sort_with

fun sort_with : (cmp: a -> a -> Ordering) -> (xs: List a) -> List a

Sorts a list using a custom comparison function.

sort_by

fun sort_by : (key: a -> b) -> (xs: List a) -> List a

Sorts a list by comparing the results of a key function.

find

fun find : (pred: a -> Bool needs {..e}) -> (xs: List a) -> Maybe a needs {..e}

Returns the first element matching the predicate, or Nothing.

contains

fun contains : (elem: a) -> (xs: List a) -> Bool where {a: Eq}

Returns True if the element is in the list.

nth

fun nth : (n: Int) -> (xs: List a) -> Maybe a

Returns the element at the given 0-based index, or Nothing if out of bounds.

zip_with

fun zip_with : (f: a -> b -> c needs {..e}) -> (xs: List a) -> (ys: List b) -> List c needs {..e}

Combines two lists element-wise using a function. Stops at the shorter list.

unzip

fun unzip : (xs: List (a, b)) -> (List a, List b)

Transforms a list of pairs into a pair of lists.

partition

fun partition : (pred: a -> Bool needs {..e}) -> (xs: List a) -> (List a, List a) needs {..e}

Splits a list into two: elements that satisfy the predicate and those that don't.

enumerate

fun enumerate : (xs: List a) -> List (Int, a)

Pairs each element with its 0-based index.

intersperse

fun intersperse : (sep: a) -> (xs: List a) -> List a

Inserts a separator between each element of a list.

unique

fun unique : (xs: List a) -> List a where {a: Eq}

Removes duplicate elements, preserving the first occurrence of each.

concat

fun concat : (xss: List (List a)) -> List a

Flattens a list of lists into a single list.

iter

fun iter : (f: a -> Unit needs {..e}) -> (xs: List a) -> Unit needs {..e}

Applies a side-effecting function to each element.

scan

fun scan : (f: b -> a -> b needs {..e}) -> (acc: b) -> (xs: List a) -> List b needs {..e}

Like foldl, but returns a list of all intermediate accumulator values.

chunks

fun chunks : (n: Int) -> (xs: List a) -> List (List a)

Splits a list into chunks of the given size. The last chunk may be smaller.

window

fun window : (n: Int) -> (xs: List a) -> List (List a)

Returns a sliding window of the given size over the list.