datastream/chunk

Chunk(a) is an opaque, finite, immutable batch of elements.

Chunks are the element type of stream operations that produce batches (stream.chunks_of, stream.group_adjacent, erlang/time.window_time, …). The type is intentionally opaque so the internal representation can change later (for example a contiguous byte buffer for Chunk(Int)) without breaking callers.

The accessor surface is intentionally narrow: to_list, size, is_empty. Anything else is expressed by going through to_list, keeping Chunk from re-mirroring the entire list API.

Chunks are finite by design. Infinite-ness is the responsibility of Stream, not of a self-contained value.

Types

A finite, immutable batch of a values.

The internal representation is hidden so the library is free to change it (currently a list, possibly a contiguous buffer later) without a breaking change. Observational equality goes through to_list.

pub opaque type Chunk(a)

Values

pub fn concat(chunks: List(Chunk(a))) -> Chunk(a)

Concatenate chunks in list order.

pub fn empty() -> Chunk(a)

The zero-element chunk.

pub fn filter(
  over chunk: Chunk(a),
  keeping predicate: fn(a) -> Bool,
) -> Chunk(a)

Keep only the elements for which predicate returns True. Order is preserved.

pub fn from_list(elements: List(a)) -> Chunk(a)

Build a chunk from a list of elements.

Round-trips with to_list: to_list(from_list(xs)) == xs.

pub fn is_empty(chunk: Chunk(a)) -> Bool

True iff chunk carries zero elements.

pub fn map(over chunk: Chunk(a), with f: fn(a) -> b) -> Chunk(b)

Apply f to every element. The result has the same size as the input; order is preserved.

pub fn singleton(value: a) -> Chunk(a)

The one-element chunk containing value.

pub fn size(chunk: Chunk(a)) -> Int

Number of elements in chunk. O(n) in the current implementation (the chunk’s element list is walked).

pub fn to_list(chunk: Chunk(a)) -> List(a)

Materialise a chunk back into a list, preserving order.

Search Document