March Docs

Iterable

Iterable module: sequence operations over List(a).

This module provides a unified set of sequence operations: map, filter, fold, take, drop, zip, enumerate, flat_map, any, all, find, count.

All functions operate on List(a). When the runtime gains interface dispatch, these will be generalised to work over any type implementing Iterable(a).

The Iterable interface (future):

interface Iterable(a) do fn next(self : Self) : Option((a, Self)) end

impl Iterable(a) for List(a) do fn next(xs : List(a)) : Option((a, List(a))) do match xs do | Nil -> None | Cons(h, t) -> Some((h, t)) end end end

Until then, every function here takes and returns List(a).

Functions

fnmapmap(xs : List(a), f : a -> b) : List(b)#

Applies f to each element, returning a new list of results.

fnflat_mapflat_map(xs : List(a), f : a -> List(b)) : List(b)#

Applies f to each element; collects the resulting lists and concatenates them.

fnfilterfilter(xs : List(a), pred : a -> Bool) : List(a)#

Returns only the elements for which pred returns true.

fnfoldfold(zero : b, xs : List(a), f : b -> a -> b) : b#

Left fold: reduces the list from left to right using an accumulator. f is called as f(acc, x) returning the new accumulator.

fncountcount(xs : List(a)) : Int#

Returns the number of elements in the list.

fnanyany(xs : List(a), pred : a -> Bool) : Bool#

Returns true if any element satisfies pred (short-circuits).

fnallall(xs : List(a), pred : a -> Bool) : Bool#

Returns true if all elements satisfy pred (short-circuits on failure).

fnfindfind(xs : List(a), pred : a -> Bool) : Option(a)#

Returns the first element satisfying pred, or None.

fntaketake(xs : List(a), n : Int) : List(a)#

Returns the first n elements. Returns the whole list if n >= length.

fndropdrop(xs : List(a), n : Int) : List(a)#

Drops the first n elements. Returns [] if n >= length.

fnzipzip(xs : List(a), ys : List(b)) : List((a, b))#

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

fnenumerateenumerate(xs : List(a)) : List((Int, a))#

Pairs each element with its 0-based index: [a, b] -> [(0, a), (1, b)].