March Docs

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

ptypeStepStep(a) = Continue(a) | Halt(a)#
ptypeSeqSeq(a) = Seq(a)#

Functions

fnallall(seq, pred)#
fnanyany(seq, pred)#
fnbatchbatch(seq, n : {Int | _ > 0})#
fnbatchedbatched(seq, n)#

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.

fnconcatconcat(s1, s2)#
fncountcount(seq)#
fncyclecycle(seq, n)#

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).
fndropdrop(seq, n)#
fneacheach(seq, f)#
fnemptyempty()#
fnfilterfilter(seq, pred)#
fnfindfind(seq, pred)#
fnflat_mapflat_map(seq, f)#
fnfoldfold(seq, start, f)#
fnfold_whilefold_while(seq, start, f)#
fnfrom_file_chunksfrom_file_chunks(path, size)#

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.).

fnfrom_file_linesfrom_file_lines(path)#

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(_) -> []
    end
fnfrom_listfrom_list(xs)#
fnfrom_string_linesfrom_string_lines(s)#

Build 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.

fniterateiterate(seed, step, n)#

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]
fnmapmap(seq, f)#
fnrepeatrepeat(x, n)#

A Seq(a) that yields x exactly n times. repeat(_, 0) is the empty seq; negative n is treated as 0.

fntaketake(seq, n)#
fnto_listto_list(seq)#
fnunfoldunfold(seed, next)#
fnunfold_untilunfold_until(seed, next, max)#

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]
fnzipzip(s1, s2)#