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 every element satisfies pred, stopping at the first failure. True for an empty list.

Examples

march> Iterable.all([1, 2, 3], fn x -> x > 0)
true
march> Iterable.all([1, -2, 3], fn x -> x > 0)
false
march> Iterable.all([], fn x -> x > 0)
true
fnanyany(xs : List(a), pred : a -> Bool) : Bool#

Returns true if any element satisfies pred, stopping at the first match. False for an empty list.

Examples

march> Iterable.any([1, 2, 3], fn x -> x > 2)
true
march> Iterable.any([], fn x -> x > 2)
false
fncountcount(xs : List(a)) : Int#

Returns the number of elements in the list. O(n).

Examples

march> Iterable.count([1, 2, 3])
3
march> Iterable.count([])
0
fndropdrop(xs : List(a), n : Int) : List(a)#

Drops the first n elements. Returns [] if n >= length and the whole list if n <= 0.

Examples

march> Iterable.drop([1, 2, 3], 2)
[3]
march> Iterable.drop([1, 2, 3], 5)
[]
march> Iterable.drop([1, 2, 3], -1)
[1, 2, 3]
fnenumerateenumerate(xs : List(a)) : List((Int, a))#

Pairs each element with its 0-based index, index first.

Examples

march> Iterable.enumerate(["a", "b"])
[(0, "a"), (1, "b")]
march> Iterable.enumerate([])
[]
fnfilterfilter(xs : List(a), pred : a -> Bool[p]) : List(a[p])#

Returns only the elements for which pred returns true, preserving order. Every element of the result is known to satisfy pred (an abstract refinement p, as for List.filter).

Examples

march> Iterable.filter([1, 2, 3, 4], fn x -> x % 2 == 0)
[2, 4]
fnfindfind(xs : List(a), pred : a -> Bool[p]) : Option(a[p])#

Returns the first element satisfying pred as Some, or None if there is none. The element, when present, is known to satisfy pred (an abstract refinement p, as for List.filter).

Examples

march> Iterable.find([1, 2, 3, 4], fn x -> x > 2)
Some(3)
march> Iterable.find([1, 2], fn x -> x > 5)
None
fnflat_mapflat_map(xs : List(a), f : a -> List(b)) : List(b)#

Applies f to each element and concatenates the resulting lists, in order. O(total output length).

Examples

march> Iterable.flat_map([1, 2, 3], fn x -> [x, x])
[1, 1, 2, 2, 3, 3]
march> Iterable.flat_map([1, 2], fn _ -> [])
[]
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. Returns zero for an empty list.

Examples

march> Iterable.fold([1, 2, 3], 0, fn (acc, x) -> acc + x)
6
march> Iterable.fold(["a", "b", "c"], "", fn (acc, x) -> acc ++ x)
"abc"
fnmapmap(xs : List(a), f : a -> b) : List(b)#

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

Examples

march> Iterable.map([1, 2, 3], fn x -> x * 10)
[10, 20, 30]
march> Iterable.map([], fn x -> x * 10)
[]
fntaketake(xs : List(a), n : Int) : List(a)#

Returns the first n elements. Returns the whole list if n >= length and [] if n <= 0.

Examples

march> Iterable.take([1, 2, 3], 2)
[1, 2]
march> Iterable.take([1, 2, 3], 5)
[1, 2, 3]
march> Iterable.take([1, 2, 3], -1)
[]
fnzipzip(xs : List(a), ys : List(b)) : List((a, b))#

Pairs up elements from two lists. Stops at the shorter list; the extra elements of the longer one are dropped.

Examples

march> Iterable.zip([1, 2, 3], ["a", "b"])
[(1, "a"), (2, "b")]