Std.Stream
Lazy, pull-based, possibly-infinite sequences built on first-class continuations.
Types
Stream
type Stream a =
| Stream (Unit -> Step a)A lazy sequence. Pull from it by forcing the inner thunk via next,
or use the combinators below.
Effects
Gen
effect Gen a {
fun yield : a -> Unit
}Producer effect. A function that performs yield! can be turned into
a Stream via from_gen.
Handlers
stream_of
handler stream_of for Gen aHandler that converts a Gen-style producer into a Stream. Each yield!
captures the continuation and packages it as the next Stream cell.
Functions
from_gen
fun from_gen : Unit -> Unit needs {Gen a} -> Stream aBuild a Stream from a generator function that uses the Gen effect.
Example: fun nats_from : Int -> Unit needs {Gen Int} nats_from n = { yield! n; nats_from (n + 1) }
let nats = Stream.from_gen (fun () -> nats_from 0)
empty
fun empty : Unit -> Stream aThe empty stream.
singleton
fun singleton : a -> Stream aA stream that yields a single value, then ends.
from_list
fun from_list : List a -> Stream aConvert a list into a stream.
repeat
fun repeat : a -> Stream aAn infinite stream that yields the same value forever.
iterate
fun iterate : a -> a -> a -> Stream aAn infinite stream produced by repeatedly applying f to the previous value.
iterate f x yields x, f x, f (f x), ...
range
fun range : Int -> Int -> Stream IntA finite stream of integers from lo (inclusive) to hi (exclusive).
range_from
fun range_from : Int -> Stream IntAn infinite stream of integers starting at lo.
next
fun next : Stream a -> Maybe (a, Stream a)Pull the next element. Returns Nothing if the stream is exhausted,
or Just (value, rest) if a value is available.
is_empty
fun is_empty : Stream a -> BoolReturns True if the stream has no more elements.
map
fun map : a -> b -> Stream a -> Stream bApply f to each element. Lazy: nothing is computed until pulled.
filter
fun filter : a -> Bool -> Stream a -> Stream aKeep only elements for which p returns True. Lazy.
flat_map
fun flat_map : a -> Stream b -> Stream a -> Stream bMap each element to a stream and concatenate the results.
append
fun append : Stream a -> Stream a -> Stream aConcatenate two streams. The second is only pulled once the first is exhausted.
zip
fun zip : Stream a -> Stream b -> Stream (a, b)Pair up two streams element-wise. Stops when either runs out.
take
fun take : Int -> Stream a -> Stream aTake the first n elements.
drop
fun drop : Int -> Stream a -> Stream aSkip the first n elements.
take_while
fun take_while : a -> Bool -> Stream a -> Stream aTake elements while p returns True; stop at the first False.
drop_while
fun drop_while : a -> Bool -> Stream a -> Stream aSkip elements while p returns True; yield the rest including the first False.
to_list
fun to_list : Stream a -> List aMaterialize the stream into a list. Will not terminate on infinite streams.
fold
fun fold : b -> a -> b -> b -> Stream a -> bLeft fold over the stream.
for_each
fun for_each : a -> Unit needs {..e} -> Stream a -> Unit needs {..e}Run f for every element, in order. The callback may use any effects.
length
fun length : Stream a -> IntCount the elements. Will not terminate on infinite streams.