Seq
Seq module: lazy fold-based sequences (church-encoded). Seq(a) wraps fn(b, fn(b, a) -> b) -> b. Enum operates eagerly on Lists; Seq operates lazily over I/O or generated sequences.
Types
Functions
Alias for batch: groups elements into lists of size n. Provided for parity with the API name used by the spec/plan; batch remains the canonical name.
Concatenate seq with itself n times. cycle(seq, 0) is empty.
Because Seq is church-encoded the underlying source is re-folded once
per cycle; do not pass a one-shot source like `from_file_lines` (each
re-fold would attempt to re-read a closed fd).Open path and return a lazy Seq(Bytes) of fixed-size chunks. The final chunk may be shorter than size if the file does not divide evenly. Returns Ok(seq) or Err(file_error). The fd is closed on EOF, so consume to completion (to_list, fold, etc.).
Open path and return a lazy Seq(String) over its lines. Returns Ok(seq) on success or Err(file_error) on failure. The file handle is closed automatically when iteration reaches EOF; consume the seq fully (or via to_list) to release the fd.
Each line is yielded with its trailing newline stripped.
match Seq.from_file_lines("data.txt") do
Ok(s) -> Seq.to_list(Seq.take(s, 10)) -- first 10 lines
Err(_) -> []
endBuild a Seq(String) from a string by splitting on newline. A single trailing newline does not produce a final empty element (so from_string_lines("a\\nb\\n") yields ["a", "b"], not ["a", "b", ""]). Blank lines in the middle are preserved.
Apply step repeatedly starting from seed, yielding n elements: [seed, step(seed), step(step(seed)), …]. iterate(_, _, 0) is empty.
Seq.iterate(1, fn x -> x * 2, 5) -- yields [1, 2, 4, 8, 16]A Seq(a) that yields x exactly n times. repeat(_, 0) is the empty seq; negative n is treated as 0.
Like unfold but with an upper bound on the number of elements produced. Stops at the smaller of (a) next returning None or (b) max elements yielded.
let s = Seq.unfold_until(0, fn n -> Some((n, n + 1)), 5)
-- Seq.to_list(s) == [0, 1, 2, 3, 4]