/// Immutable singly linked lists (`List` and its literals are in the /// prelude). import std/array pub fn length :: xs: List -> Int { count xs 0 } fn count :: xs acc { match xs { [] -> acc, [_, ..rest] -> count rest (acc + 1) } } pub fn reverse :: xs: List -> List { reverse_onto xs [] } fn reverse_onto :: xs acc { match xs { [] -> acc, [x, ..rest] -> reverse_onto rest [x, ..acc] } } pub fn map :: xs: List f: fn(a) -> b / -> List / { match xs { [] -> [], [x, ..rest] -> [f x, ..map rest f] } } pub fn filter :: xs: List keep: fn(a) -> Bool / -> List / { match xs { [] -> [] , [x, ..rest] -> if keep x { [x, ..filter rest keep] } else { filter rest keep } } } pub fn fold :: xs: List acc: b f: fn(b, a) -> b / -> b / { match xs { [] -> acc, [x, ..rest] -> fold rest (f acc x) f } } pub fn each :: xs: List f: fn(a) -> () / -> () / { match xs { [] -> () , [x, ..rest] -> { f x each rest f } } } pub fn append :: xs: List ys: List -> List { match xs { [] -> ys, [x, ..rest] -> [x, ..append rest ys] } } /// At most the first `n` elements: all of `xs` when `n >= length(xs)`, /// `[]` when `n <= 0`. pub fn take :: xs: List n: Int -> List { if n <= 0 { [] } else { match xs { [] -> [], [x, ..rest] -> [x, ..take rest (n - 1)] } } } /// `xs` without its first `n` elements: `[]` when `n >= length(xs)`, `xs` /// itself when `n <= 0`. pub fn drop :: xs: List n: Int -> List { if n <= 0 { xs } else { match xs { [] -> [], [_, ..rest] -> drop rest (n - 1) } } } pub fn sum :: xs: List -> a where Num { match xs { [] -> panic "list.sum of an empty list" , [x, ..rest] -> fold rest x fn acc y { acc + y } } } /// `[from, from + 1, ..., to]`; empty when `to < from`. pub fn range :: from: Int to: Int -> List { range_down to from [] } fn range_down :: to from acc { if to < from { acc } else { range_down (to - 1) from [to, ..acc] } } /// Whether `test` holds for some element. pub fn any :: xs: List test: fn(a) -> Bool / -> Bool / { match xs { [] -> False, [x, ..rest] -> test x || any rest test } } /// Whether `test` holds for every element. pub fn all :: xs: List test: fn(a) -> Bool / -> Bool / { match xs { [] -> True, [x, ..rest] -> test x && all rest test } } /// The first element for which `test` holds, if any. pub fn find :: xs: List test: fn(a) -> Bool / -> Option / { match xs { [] -> None, [x, ..rest] -> if test x { Some(x) } else { find rest test } } } /// `f` applied to each element, the resulting lists concatenated in /// order (Haskell's `concatMap`). pub fn flat_map :: xs: List f: fn(a) -> List / -> List / { match xs { [] -> [], [x, ..rest] -> append (f x) (flat_map rest f) } } /// `f` applied to each element, while it succeeds: `Ok` with the /// values, or the first `Error`, after which `f` is not called again. pub fn try_map :: xs: List f: fn(a) -> Result / -> Result, x> / { match xs { [] -> Ok([]) , [x, ..rest] -> match f x { Error(err) -> Error(err) , Ok(y) -> match try_map rest f { Ok(ys) -> Ok([y, ..ys]), Error(err) -> Error(err) } } } } /// The elements of `xs` and `ys` in pairs, up to the shorter list. pub fn zip :: xs: List ys: List -> List<(a, b)> { match (xs, ys) { ([x, ..xs], [y, ..ys]) -> [(x, y), ..zip xs ys], _ -> [] } } /// The first and the second elements of the pairs, as two lists. pub fn unzip :: pairs: List<(a, b)> -> (List, List) { match pairs { [] -> ([], []) , [(x, y), ..rest] -> { let (xs, ys) = unzip rest ([x, ..xs], [y, ..ys]) } } } /// `f` applied to the elements of `xs` and `ys` in pairs, up to the /// shorter list. pub fn zip_with :: xs: List ys: List f: fn(a, b) -> c / -> List / { match (xs, ys) { ([x, ..xs], [y, ..ys]) -> [f x y, ..zip_with xs ys f], _ -> [] } } /// `xs` in ascending order. Stable: elements that compare `Eq` keep /// their original relative order. pub fn sort :: xs: List -> List where Ord { sort_by xs fn x y { compare x y } } /// `xs` ordered by `compare` (`Lt` means "comes first"). Stable, as /// `sort` is. /// /// Sorts through `std/array`'s driftsort-style sort (issue #380). /// Measured against the top-down merge sort it replaced /// (tests/bench/sorts.lse, 100,000 elements, user instructions on one /// core, conversions included), the roundtrip wins on structured input /// and ties on random: random 731M -> 717M, low-cardinality 721M -> /// 392M, and, being run-adaptive where the merge sort was not, sorted /// 342M -> 44M, reverse 341M -> 51M, organ-pipe 361M -> 78M. It costs /// one O(n) conversion each way, and it is slower in the interpreter /// (about 8x on random data, where big sorts are rare); native is the /// target that matters. Which comparisons happen, and in what order, /// differs from the old sort: an effectful comparator observes a /// different sequence. pub fn sort_by :: xs: List compare: fn(a, a) -> Order / -> List / { array.to_list (array.sort_by (array.from_list xs) compare) }