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
endUntil then, every function here takes and returns List(a).
Functions
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)
trueReturns 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)
falseReturns the number of elements in the list. O(n).
Examples
march> Iterable.count([1, 2, 3])
3
march> Iterable.count([])
0Drops 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]Pairs each element with its 0-based index, index first.
Examples
march> Iterable.enumerate(["a", "b"])
[(0, "a"), (1, "b")]
march> Iterable.enumerate([])
[]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]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)
NoneApplies 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 _ -> [])
[]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"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)
[]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)
[]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")]