datastream/sink

Terminal consumers over Stream(a) — pure reductions and side-effecting drivers.

sink is the unified terminal layer: every operation that drives a stream to completion (or until an early-exit decision) lives here. Pure reductions (to_list, count, fold, …) and side-effecting consumers (each, try_each, println) sit side-by-side; the import path is one line regardless of whether the caller’s terminal returns a value or runs a Nil-typed effect.

The pure reductions are also exported from datastream/fold for backward compatibility with code written against earlier versions of this library. Prefer the datastream/sink import path for new code; the fold aliases are kept for now and may be removed in a future major release.

Values

pub fn all(
  over stream: datastream.Stream(a),
  satisfying predicate: fn(a) -> Bool,
) -> Bool

Return True when every element satisfies predicate; True for an empty stream. Short-circuits on the first False. Re-exported from datastream/fold.all.

pub fn any(
  over stream: datastream.Stream(a),
  satisfying predicate: fn(a) -> Bool,
) -> Bool

Return True when at least one element satisfies predicate; False for an empty stream. Short-circuits on the first True. Re-exported from datastream/fold.any.

pub fn collect_result(
  stream: datastream.Stream(Result(a, e)),
) -> Result(List(a), e)

Collect a Stream(Result(a, e)) into Result(List(a), e) with short-circuit semantics on the first Error(e). Re-exported from datastream/fold.collect_result.

pub fn count(stream: datastream.Stream(a)) -> Int

Count the number of elements the stream produces. Re-exported from datastream/fold.count.

pub fn drain(stream: datastream.Stream(a)) -> Nil

Drive the stream to completion and discard every element. Re-exported from datastream/fold.drain.

pub fn each(
  over stream: datastream.Stream(a),
  with effect: fn(a) -> Nil,
) -> Nil

Drive the stream to completion, calling effect once per element in source order. Returns Nil.

Use this when the work the consumer does is infallible. For fallible consumers use try_each.

pub fn find(
  in stream: datastream.Stream(a),
  satisfying predicate: fn(a) -> Bool,
) -> option.Option(a)

Return the first element that satisfies predicate, or None. Re-exported from datastream/fold.find.

pub fn first(stream: datastream.Stream(a)) -> option.Option(a)

Pull until the first element and return it as Some(a), or None for an empty stream. Re-exported from datastream/fold.first.

pub fn fold(
  over stream: datastream.Stream(a),
  from initial: acc,
  with step: fn(acc, a) -> acc,
) -> acc

Left-fold the stream with an explicit accumulator. Re-exported from datastream/fold.fold.

pub fn last(stream: datastream.Stream(a)) -> option.Option(a)

Drive the stream to completion and return the final element as Some(a), or None for an empty stream. Re-exported from datastream/fold.last.

pub fn partition_map(
  over stream: datastream.Stream(a),
  with split: fn(a) -> Result(b, c),
) -> #(List(b), List(c))

Map every element to a Result(b, c) and partition the outcomes into a (lefts, rights) pair. Re-exported from datastream/fold.partition_map.

pub fn partition_result(
  stream: datastream.Stream(Result(a, e)),
) -> #(List(a), List(e))

Partition a Stream(Result(a, e)) into a (oks, errs) pair. Re-exported from datastream/fold.partition_result.

pub fn println(stream: datastream.Stream(String)) -> Nil

Print every element of stream followed by a newline to standard output, in source order.

Cross-target convenience over gleam/io.println. Target-aware sinks (file, socket, …) belong outside the cross-target core.

pub fn product_int(stream: datastream.Stream(Int)) -> Int

Multiply every Int element. Returns 1 for an empty stream. Re-exported from datastream/fold.product_int.

pub fn reduce(
  over stream: datastream.Stream(a),
  with step: fn(a, a) -> a,
) -> option.Option(a)

Left-fold using the first element as the seed; returns None on an empty stream. Re-exported from datastream/fold.reduce.

pub fn sum_float(stream: datastream.Stream(Float)) -> Float

Sum every Float element. Returns 0.0 for an empty stream. Re-exported from datastream/fold.sum_float.

pub fn sum_int(stream: datastream.Stream(Int)) -> Int

Sum every Int element. Returns 0 for an empty stream. Re-exported from datastream/fold.sum_int.

pub fn to_bit_array(
  stream: datastream.Stream(BitArray),
) -> BitArray

Materialise a Stream(BitArray) into a single concatenated BitArray. Re-exported from datastream/fold.to_bit_array.

pub fn to_list(stream: datastream.Stream(a)) -> List(a)

Materialise the stream into a list, preserving source order. Re-exported from datastream/fold.to_list.

pub fn to_string(stream: datastream.Stream(String)) -> String

Materialise a Stream(String) into a single concatenated String. Re-exported from datastream/fold.to_string.

pub fn to_string_join(
  stream stream: datastream.Stream(String),
  with separator: String,
) -> String

Materialise a Stream(String) into a single String with separator between consecutive elements. Streaming counterpart of gleam/string.join/2. Re-exported from datastream/fold.to_string_join. (#213)

pub fn to_string_tree(
  stream: datastream.Stream(String),
) -> string_tree.StringTree

Materialise a Stream(String) into a StringTree, preserving source order. Re-exported from datastream/fold.to_string_tree.

pub fn to_string_tree_join(
  stream stream: datastream.Stream(String),
  with separator: String,
) -> string_tree.StringTree

StringTree variant of to_string_join. Re-exported from datastream/fold.to_string_tree_join. (#213)

pub fn try_each(
  over stream: datastream.Stream(a),
  with effect: fn(a) -> Result(Nil, e),
) -> Result(Nil, e)

Drive the stream while effect returns Ok(Nil), halting on the first Error(e) and returning that error.

On all-Ok returns Ok(Nil). On the first Error(e), no further element is pulled from upstream and the upstream’s close callback is invoked before Error(e) is returned, so resource-backed sources are released even on the early-exit path.

Search Document