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

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

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

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

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

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

Returns the number of elements in the list.

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

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

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

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

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

Returns only the elements for which pred returns true.

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

Returns the first element satisfying pred, or None.

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

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

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

Left fold: reduces the list from left to right using an accumulator. Argument order: collection first, init second, callback last (uncurried-collection convention). Callback f(acc, x) returns the new accumulator.

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

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

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

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

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

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