Stream
Config & IO
Composable lazy enumerables for the BEAM target.
Like Elixir's Stream, these functions build a recipe. Nothing runs until you consume with foldl, toList, run, attempt, or attemptFoldl. Chain map / filter / take without allocating intermediate lists.
For large inputs (especially File.stream), prefer foldl, tryFoldl, stoppableFoldl, or attemptFoldl so only one element (or chunk) is live at a time. toList, attempt, and cycle materialize; avoid them on huge streams.
HTTP request body streams (Server.bodyStream, Multipart.fileStream) must be consumed synchronously inside handle with tryFoldl / stoppableFoldl — not attemptFoldl (tasks run after the response is sent).
From update, fold a file in a task:
File.stream path
|> Stream.map parseRow
|> Stream.filter isValid
|> Stream.attemptFoldl (\_ n -> n + 1) 0
|> Task.attempt GotCountTypes
Stream
type Stream a = StreamA suspended sequence of a values.
Examples
Stream.fromList [ 1, 2, 3 ]Error
type Error = ReadFailed StringWhy consuming a resource-backed stream failed.
Examples
Err (Stream.ReadFailed "enoent")Values
errorToString
errorToString : Error -> StringHuman-readable form of an Error for logs.
Examples
Stream.errorToString errempty
empty : Stream aA stream with no elements.
Examples
Stream.emptyfromList
fromList : List a -> Stream aLift an eager list into a stream.
Examples
Stream.fromList [ 1, 2, 3 ]unfold
unfold : (s -> Maybe (a, s)) -> s -> Stream aBuild a stream from a seed. Nothing ends the stream.
Examples
Stream.unfold (\n -> if n > 3 then Nothing else Just (n, n + 1)) 1iterate
iterate : (a -> a) -> a -> Stream aInfinite stream: x, f x, f (f x), ...
Examples
Stream.iterate (\n -> n + 1) 0range
range : Int -> Int -> Stream IntInclusive integer range, like List.range. Empty when lo > hi.
Examples
Stream.range 1 10repeat
repeat : a -> Stream aInfinite stream of the same value.
Examples
Stream.repeat 0repeatedly
repeatedly : (() -> a) -> Stream aInfinite stream from a thunk called for each element.
Examples
Stream.repeatedly (\() -> Time.now)cycle
cycle : Stream a -> Stream aRepeat a finite stream forever. Buffers the whole stream once; empty stays empty.
Examples
Stream.cycle (Stream.fromList [ 1, 2 ])resource
resource : (() -> s) -> (s -> Maybe (a, s)) -> (s -> ()) -> Stream aEmit values from a resource. start runs on first pull, after on exhaust or early stop (take, discarded tail).
Examples
Stream.resource open next closemap
map : (a -> b) -> Stream a -> Stream bLazily transform each element.
Examples
Stream.map String.toUpper linesfilter
filter : (a -> Bool) -> Stream a -> Stream aLazily keep elements that pass the predicate.
Examples
Stream.filter (\n -> n > 0) numsreject
reject : (a -> Bool) -> Stream a -> Stream aLazily drop elements that pass the predicate.
Examples
Stream.reject String.isEmpty linesfilterMap
filterMap : (a -> Maybe b) -> Stream a -> Stream bLazily map and drop Nothing results.
Examples
Stream.filterMap String.toInt linesflatMap
flatMap : (a -> Stream b) -> Stream a -> Stream bLazily map to an inner stream and flatten.
Examples
Stream.flatMap (\n -> Stream.range 1 n) numstake
take : Int -> Stream a -> Stream aLazily take the first n elements. Negative or zero yields empty.
Examples
Stream.take 10 infinitedrop
drop : Int -> Stream a -> Stream aLazily skip the first n elements.
Examples
Stream.drop 1 linestakeWhile
takeWhile : (a -> Bool) -> Stream a -> Stream aLazily take while the predicate holds; then stop and release the rest.
Examples
Stream.takeWhile (\n -> n < 10) numsdropWhile
dropWhile : (a -> Bool) -> Stream a -> Stream aLazily skip while the predicate holds.
Examples
Stream.dropWhile String.isEmpty linesconcat
concat : Stream a -> Stream a -> Stream aLazily emit left then right.
Examples
Stream.concat left rightconcatAll
concatAll : Stream (Stream a) -> Stream aFlatten a stream of streams.
Examples
Stream.concatAll (Stream.fromList [ a, b ])zip
zip : Stream a -> Stream b -> Stream (a, b)Lazily pair elements until either stream ends.
Examples
Stream.zip names ageswithIndex
withIndex : Stream a -> Stream (Int, a)Pair each element with its zero-based index.
Examples
Stream.withIndex linesintersperse
intersperse : a -> Stream a -> Stream aLazily insert a separator between elements.
Examples
Stream.intersperse "," partsscan
scan : (a -> b -> b) -> b -> Stream a -> Stream bLazily scan: each emitted value is the new accumulator.
Examples
Stream.scan (\n acc -> acc + n) 0 numseach
each : (a -> ()) -> Stream a -> Stream aRun an effect for each element when the stream is later consumed.
Examples
Stream.each (\line -> Debug.log "line" line) lineschunkEvery
chunkEvery : Int -> Stream a -> Stream (List a)Lazily emit lists of at most n elements. The last chunk may be shorter. Zero or negative n yields empty.
Examples
Stream.chunkEvery 100 rowsfoldl
foldl : (a -> b -> b) -> b -> Stream a -> bReduce a stream left-to-right. Raises on resource failure; see tryFoldl.
Examples
Stream.foldl (\_ n -> n + 1) 0 streamtoList
toList : Stream a -> List aMaterialize the whole stream as a list. Avoid on huge inputs.
Examples
Stream.toList (Stream.range 1 3)run
run : Stream a -> ()Drain a stream for its effects; discard values.
Examples
Stream.run effectstryFoldl
tryFoldl : (a -> b -> b) -> b -> Stream a -> Result Error bLike foldl, but resource failures become Err.
Examples
Stream.tryFoldl (\_ n -> n + 1) 0 (File.stream path)stoppableFoldl
stoppableFoldl : (a -> b -> FoldStep b) -> b -> Stream a -> Result Error bLeft fold that can stop early via List.FoldStep. On Stop, the rest of the stream is discarded (releasing files / HTTP body resources). I/O failures are Err.
Examples
Stream.stoppableFoldl (\row n -> List.Continue (n + 1)) 0 rowstryToList
tryToList : Stream a -> Result Error (List a)Like toList, but resource failures become Err.
Examples
Stream.tryToList (File.stream path)attempt
attempt : Stream a -> Task Error (List a)Consume with tryToList as a Task for TEA. Prefer attemptFoldl for large streams.
Examples
Stream.attempt (File.stream path)attemptFoldl
attemptFoldl : (a -> b -> b) -> b -> Stream a -> Task Error bConsume with tryFoldl as a Task for TEA. Constant memory for large files.
Examples
Stream.attemptFoldl (\_ n -> n + 1) 0 (File.stream path)