Std.Iter (Cure Standard Library v0.37.2)

View Source

Lazy iterators.

Cure comprehensions and Std.List are eager: every step materialises a new cons list. Std.Iter is the lazy half of the collections story. An iterator is an inductive wrapper around a step function. Each step returns either Some(Yield(element, next)) or None(), so the element and continuation stay typed without falling back to Any.

The lazy idiom

The recommended way to spell a lazy pipeline is to mark the entry point with lazy/1 and let the rest of the chain stay in Std.Iter:

use Std.Iter

# Squares of the first five even integers in [1, 10].
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  |> lazy
  |> filter(fn(x) -> x % 2 == 0)
  |> map(fn(x) -> x * x)
  |> take(5)
# => [4, 16, 36, 64, 100]

Nothing materialises until the terminal take/2 (which returns a plain list). For an open-ended chain, end with to_list/1 -- guard infinite producers with take/2 or take_while/2 first.

Examples

use Std.Iter

# Sum of the integers in [1, 10] via the iterator API.
fold(range(1, 10), 0, fn(x) -> fn(acc) -> acc + x)
# => 55
use Std.Iter

# First five Fibonacci numbers from an infinite producer.
let pair_step = fn(p) ->
  match p
    %[a, b] -> Some(Emit(a, %[b, a + b]))
unfold(%[0, 1], pair_step) |> take(5)
# => [0, 1, 1, 2, 3]

Summary

Functions

true when every element satisfies pred (vacuously true for the empty iterator). Stops at the first miss.

true when at least one element satisfies pred. Stops at the first hit.

Concatenate two iterators. Stepping b is deferred until a is exhausted.

Count elements satisfying pred. Tail-recursive.

Infinite iterator that walks list repeatedly. An empty input collapses to empty/0 so consumers terminate.

Private: step cycle/1 by tracking the unspent suffix and the original list to restart from.

Skip the first n elements. Negative or zero n is a no-op.

Private: walk n elements past the iterator's current position.

Skip elements while pred holds, then yield the rest unchanged.

Apply f to every element for its side effects. Returns Done once the iterator is exhausted.

Private: ignore f's return value and continue iterating.

An iterator that is immediately exhausted.

Keep elements of it for which pred(elem) is true.

First element satisfying pred; returns default when nothing matches. Stops at the first hit.

Map f over it and flatten one level of nesting; f returns an iterator per element.

Left fold an iterator. f is a curried function elem -> acc -> acc.

An iterator over a list (walks left to right).

Insert sep between consecutive elements of it. Empty and single-element iterators are passed through unchanged.

Private: keep emitting sep, elem pairs after the first element.

Infinite iterator: x0, f(x0), f(f(x0)), .... Always pair with a slicer such as take/2 or take_while/2 before forcing.

Bridge for the lazy-pipeline idiom: xs |> lazy |> map(...) |> .... Identical to from_list/1; the alternate name documents intent at the call site.

Map f over every element of it, on demand.

Private: prepend a single element to an existing iterator.

A bounded integer range iterator (inclusive).

Private: single step of the range/2 iterator.

Infinite iterator that yields x forever.

Take at most n values from the iterator as a plain list. Safe to call on infinite iterators.

Lazy prefix: keep elements while pred holds, then terminate.

Materialise an iterator as a list. Non-terminating iterators never return; guard with take/2 when unsure.

Stream-style unfold. f(seed) returns either Some(Emit(value, next_seed)) to emit value and continue, or None() to terminate.

Combine two iterators element-wise using a curried f. Stops at the shorter iterator. f is a -> b -> c.

Types

type EachDone

 type EachDone

enum Iter

 enum Iter
   Iter

enum IterStep

 enum IterStep
   Yield

type StepToken

 type StepToken

enum UnfoldStep

 enum UnfoldStep
   Emit

Functions

all(it: Iter(t), pred: Function(t, Bool)) -> Bool

 fn all(it: Iter(t), pred: Function(t, Bool)) -> Bool

true when every element satisfies pred (vacuously true for the empty iterator). Stops at the first miss.

any(it: Iter(t), pred: Function(t, Bool)) -> Bool

 fn any(it: Iter(t), pred: Function(t, Bool)) -> Bool

true when at least one element satisfies pred. Stops at the first hit.

concat(a: Iter(t), b: Iter(t)) -> Iter(t)

 fn concat(a: Iter(t), b: Iter(t)) -> Iter(t)

Concatenate two iterators. Stepping b is deferred until a is exhausted.

count(it: Iter(t), pred: Function(t, Bool)) -> Int

 fn count(it: Iter(t), pred: Function(t, Bool)) -> Int

Count elements satisfying pred. Tail-recursive.

count_acc(it: Iter(t), pred: Function(t, Bool), acc: Int) -> Int

 fn count_acc(it: Iter(t), pred: Function(t, Bool), acc: Int) -> Int

Private accumulator helper for count/2.

cycle(list: List(t)) -> Iter(t)

 fn cycle(list: List(t)) -> Iter(t)

Infinite iterator that walks list repeatedly. An empty input collapses to empty/0 so consumers terminate.

cycle_step(remaining: List(t), original: List(t)) -> Iter(t)

 fn cycle_step(remaining: List(t), original: List(t)) -> Iter(t)

Private: step cycle/1 by tracking the unspent suffix and the original list to restart from.

drop(it: Iter(t), n: Int) -> Iter(t)

 fn drop(it: Iter(t), n: Int) -> Iter(t)

Skip the first n elements. Negative or zero n is a no-op.

drop_step(it: Iter(t), n: Int) -> Option(IterStep(t))

 fn drop_step(it: Iter(t), n: Int) -> Option(IterStep(t))

Private: walk n elements past the iterator's current position.

drop_while(it: Iter(t), pred: Function(t, Bool)) -> Iter(t)

 fn drop_while(it: Iter(t), pred: Function(t, Bool)) -> Iter(t)

Skip elements while pred holds, then yield the rest unchanged.

drop_while_step(it: Iter(t), pred: Function(t, Bool)) -> Option(IterStep(t))

 fn drop_while_step(it: Iter(t), pred: Function(t, Bool)) -> Option(IterStep(t))

Private: drive drop_while/2 until the first failing element.

each(it: Iter(t), f: Function(t, u)) -> EachDone

 fn each(it: Iter(t), f: Function(t, u)) -> EachDone

Apply f to every element for its side effects. Returns Done once the iterator is exhausted.

each_after(_value: u, rest: Iter(t), f: Function(t, u)) -> EachDone

 fn each_after(_value: u, rest: Iter(t), f: Function(t, u)) -> EachDone

Private: ignore f's return value and continue iterating.

empty() -> Iter(t)

 fn empty() -> Iter(t)

An iterator that is immediately exhausted.

filter(it: Iter(t), pred: Function(t, Bool)) -> Iter(t)

 fn filter(it: Iter(t), pred: Function(t, Bool)) -> Iter(t)

Keep elements of it for which pred(elem) is true.

filter_step(it: Iter(t), pred: Function(t, Bool)) -> Option(IterStep(t))

 fn filter_step(it: Iter(t), pred: Function(t, Bool)) -> Option(IterStep(t))

Private: drive filter/2 until a passing element is found.

find(it: Iter(t), pred: Function(t, Bool), default: t) -> t

 fn find(it: Iter(t), pred: Function(t, Bool), default: t) -> t

First element satisfying pred; returns default when nothing matches. Stops at the first hit.

flat_map(it: Iter(t), f: Function(t, Iter(u))) -> Iter(u)

 fn flat_map(it: Iter(t), f: Function(t, Iter(u))) -> Iter(u)

Map f over it and flatten one level of nesting; f returns an iterator per element.

fold(it: Iter(t), acc: u, f: Function(t, Function(u, u))) -> u

 fn fold(it: Iter(t), acc: u, f: Function(t, Function(u, u))) -> u

Left fold an iterator. f is a curried function elem -> acc -> acc.

from_list(list: List(t)) -> Iter(t)

 fn from_list(list: List(t)) -> Iter(t)

An iterator over a list (walks left to right).

intersperse(it: Iter(t), sep: t) -> Iter(t)

 fn intersperse(it: Iter(t), sep: t) -> Iter(t)

Insert sep between consecutive elements of it. Empty and single-element iterators are passed through unchanged.

intersperse_tail(it: Iter(t), sep: t) -> Iter(t)

 fn intersperse_tail(it: Iter(t), sep: t) -> Iter(t)

Private: keep emitting sep, elem pairs after the first element.

iterate(x0: t, f: Function(t, t)) -> Iter(t)

 fn iterate(x0: t, f: Function(t, t)) -> Iter(t)

Infinite iterator: x0, f(x0), f(f(x0)), .... Always pair with a slicer such as take/2 or take_while/2 before forcing.

lazy(list: List(t)) -> Iter(t)

 fn lazy(list: List(t)) -> Iter(t)

Bridge for the lazy-pipeline idiom: xs |> lazy |> map(...) |> .... Identical to from_list/1; the alternate name documents intent at the call site.

map(it: Iter(t), f: Function(t, u)) -> Iter(u)

 fn map(it: Iter(t), f: Function(t, u)) -> Iter(u)

Map f over every element of it, on demand.

prepend(x: t, it: Iter(t)) -> Iter(t)

 fn prepend(x: t, it: Iter(t)) -> Iter(t)

Private: prepend a single element to an existing iterator.

range(lo: Int, hi: Int) -> Iter(Int)

 fn range(lo: Int, hi: Int) -> Iter(Int)

A bounded integer range iterator (inclusive).

range_step(lo: Int, hi: Int) -> Option(IterStep(Int))

 fn range_step(lo: Int, hi: Int) -> Option(IterStep(Int))

Private: single step of the range/2 iterator.

repeat(x: t) -> Iter(t)

 fn repeat(x: t) -> Iter(t)

Infinite iterator that yields x forever.

step(it: Iter(t)) -> Option(IterStep(t))

 fn step(it: Iter(t)) -> Option(IterStep(t))

take(it: Iter(t), n: Int) -> List(t)

 fn take(it: Iter(t), n: Int) -> List(t)

Take at most n values from the iterator as a plain list. Safe to call on infinite iterators.

take_while(it: Iter(t), pred: Function(t, Bool)) -> Iter(t)

 fn take_while(it: Iter(t), pred: Function(t, Bool)) -> Iter(t)

Lazy prefix: keep elements while pred holds, then terminate.

to_list(it: Iter(t)) -> List(t)

 fn to_list(it: Iter(t)) -> List(t)

Materialise an iterator as a list. Non-terminating iterators never return; guard with take/2 when unsure.

unfold(seed: s, f: Function(s, Option(UnfoldStep(t, s)))) -> Iter(t)

 fn unfold(seed: s, f: Function(s, Option(UnfoldStep(t, s)))) -> Iter(t)

Stream-style unfold. f(seed) returns either Some(Emit(value, next_seed)) to emit value and continue, or None() to terminate.

zip_with(a: Iter(t), b: Iter(u), f: Function(t, Function(u, v))) -> Iter(v)

 fn zip_with(a: Iter(t), b: Iter(u), f: Function(t, Function(u, v))) -> Iter(v)

Combine two iterators element-wise using a curried f. Stops at the shorter iterator. f is a -> b -> c.