stream

A library of channel-backed sources, transforms, and sinks. Streams are simply Channel values used as data flows — the underlying primitive is unchanged. Each source and each transform is a stage: a task that reads its input channel, calls the user closure, and writes to the output channel. A stage waits like any task, so a pipeline of many stages holds no thread while it waits. A transform takes a value from its input only when its reader asks for one. A sink that reads to the end asks for every value, so the stages before it work up to 16 values ahead while it reads. A source works ahead of its reader, up to 16 values, and stream.buffered(n) lets the stages before it work up to n values ahead.

Sinks (collect, fold, length, etc.) drain a channel in the calling task, which waits for each value.

See also io / fs for the underlying file operations behind file_chunks / file_lines, tcp for tcp_chunks / tcp_lines, and channel / task for the primitive channel operations.

Summary

Sources

FunctionSignatureDescription
from_list(List(a)) -> Channel(a)Emit list elements then close
from_range(Int, Int) -> Channel(Int)Emit lo..=hi then close
repeat(a) -> Channel(a)Infinite — pair with take
unfold(a, Fn(a) -> Option((b, a))) -> Channel(b)Generator (closes on None)
from_fn(Fn(Channel(a)) -> ()) -> Channel(a)Emit what feed sends on its channel; close when it returns
file_chunks(String, Int) -> Channel(Result(Bytes, IoError))Read file in chunks
file_lines(String) -> Channel(Result(String, IoError))Read file line-by-line
stdin_lines() -> Channel(Result(String, IoError))Read stdin line-by-line
tcp_chunks(TcpStream, Int) -> Channel(Result(Bytes, TcpError))Read TCP in chunks
tcp_lines(TcpStream) -> Channel(Result(String, TcpError))Read TCP line-by-line

A stream’s channel is only read: a send into it (channel.send, channel.try_send, a Send arm of channel.select) is a runtime error, cannot send into a stream: channel 4 is the output of stream.from_range.

file_lines reads the file up to 64 KiB of lines at a time and emits them one by one, so it can be up to about 64 KiB ahead of what has been received: a file that changes while it is read gives lines from before the change as far as it is ahead. tcp_lines likewise reads the connection up to 64 KiB at a time and emits the lines that read completed one by one; what came after the last of them waits for the next read. Under a step budget, a program reading this way uses fewer steps, since the source waits once per read, not once per line.

stdin_lines gives the lines of stdin, without their \n or \r\n, and ends at the end of stdin; the text after the last \n is the last line. A read takes what stdin has at that moment, up to 64 KiB, so a pipe gives many lines a read and a line typed at a terminal comes as soon as it is typed. A line that is not UTF-8 ends the stream with Err(InvalidInput(...)) after the lines before it, as for file_lines; its number counts the lines this stream gave. A line of stdin_lines or file_lines is as long as the input makes it, as for io.read_line and io.read_file; a line of tcp_lines is at most 10,000,000 bytes, since the peer chooses it (see tcp). A stream reads ahead of what it gives: once a program has made one, the rest of stdin is its own, and an io.read_line or io.read_stdin after it does not see what the stream has read but not given.

Transforms

FunctionSignature
map(Channel(a), Fn(a) -> b) -> Channel(b)
filter(Channel(a), Fn(a) -> Bool) -> Channel(a)
flat_map(Channel(a), Fn(a) -> List(b)) -> Channel(b)
take(Channel(a), Int) -> Channel(a)
drop(Channel(a), Int) -> Channel(a)
take_while(Channel(a), Fn(a) -> Bool) -> Channel(a)
drop_while(Channel(a), Fn(a) -> Bool) -> Channel(a)
chunks(Channel(a), Int) -> Channel(List(a))
scan(Channel(a), b, Fn(b, a) -> b) -> Channel(b)
enumerate(Channel(a)) -> Channel((Int, a))
dedup(Channel(a)) -> Channel(a) where a: Equal
buffered(Channel(a), Int) -> Channel(a)

buffered(ch, n) passes every value on. It is the one transform that takes values before they are asked for, whoever reads it, up to n of them, so the stages before it can run while its reader is still busy with an earlier value. buffered(ch, 0) reads nothing ahead.

enumerate(ch) pairs each value with its index, counting from 0 as list.enumerate does: (0, a), (1, b), …

Combinators

FunctionSignature
merge(List(Channel(a))) -> Channel(a)
concat(List(Channel(a))) -> Channel(a)
zip(Channel(a), Channel(b)) -> Channel((a, b))

Sinks

FunctionSignature
collect(Channel(a)) -> List(a)
fold(Channel(a), b, Fn(b, a) -> b) -> b
fold_until(Channel(a), b, Fn(b, a) -> Step(b)) -> b
length(Channel(a)) -> Int
first(Channel(a)) -> Option(a)
last(Channel(a)) -> Option(a)
write_to_file(Channel(Bytes), String) -> Result((), IoError)
write_to_tcp(Channel(Bytes), TcpStream) -> Result((), TcpError)

Examples

Three-step pipeline

import stream

fn main() {
  let squares = stream.from_range(1, 100)
    |> stream.filter { n -> n % 2 == 1 }
    |> stream.map { n -> n * n }
    |> stream.take(5)
    |> stream.collect
  println(squares)
}
open in playground

The lines of stdin

import stream
import string

fn main() {
  -- Count the lines of stdin that are not empty.
  let filled = stream.stdin_lines()
    |> stream.filter { line ->
      match line {
        Ok(text) -> !string.is_empty(string.trim(text))
        Err(_) -> true
      }
    }
    |> stream.length
  println("{filled} lines with text")
}
open in playground

Generator via unfold

import stream

fn main() {
  -- Generate 1, 2, 3, 4, 5 then None.
  let xs = stream.collect(
    stream.unfold(1) { n ->
      match n > 5 {
        true -> None
        false -> Some((n, n + 1))
      }
    },
  )
  println(xs)
}
open in playground

Writing a stream of Result rows to a file

stream.write_to_file takes a channel of Bytes. A source that can fail on the way, such as postgres.stream (a channel of Result(Row, PostgresError)), is written through a relay, a stream.from_fn whose feed receives the rows, sends the text of each Ok row on as bytes, and stops at the first Err; it leaves its count, or the Err, in a channel of its own. The file is written while the relay runs, so the rows are never all held. Closing the source at the Err stops what feeds it.

import bytes
import channel
import io
import result
import stream
import string

type ExportError {
  BadRow(String),
  Write(io.IoError),
}

-- Writes each Ok row to `path` as a line, as the rows come, and stops at
-- the first Err: Ok of the number of rows written, or the Err.
fn write_rows(rows: Channel(Result(String, String)), path: String) -> Result(Int, ExportError) {
  let outcome = channel.new(1)
  let lines = stream.from_fn { out ->
    let pass = { n ->
      match channel.receive(rows) {
        None -> Ok(n)
        Some(Err(e)) -> {
          channel.close(rows)
          Err(BadRow(e))
        }
        Some(Ok(row)) -> {
          channel.send(out, bytes.from_string("{row}\n"))
          pass(n + 1)
        }
      }
    }
    channel.send(outcome, pass(0))
  }
  -- A write that fails reads no further, and the relay is stopped.
  match stream.write_to_file(lines, path) {
    Ok(()) -> match channel.receive(outcome) {
      Some(sent) -> sent
      None -> Ok(0)
    }
    Err(e) -> Err(Write(e))
  }
}

fn main() {
  let good = stream.from_list([Ok("a,1"), Ok("b,2")])
  println(write_rows(good, "out.csv")) -- Ok(2)
  let bad = stream.from_list([Ok("a,1"), Err("row 2: no price"), Ok("c,3")])
  println(write_rows(bad, "out.csv")) -- Err(BadRow("row 2: no price"))
  -- The rows before the Err are in the file.
  println(io.read_file("out.csv") |> result.map(string.lines)) -- Ok(["a,1"])
}
open in playground

The lines of a file that has lines that are no UTF-8

stream.file_lines ends at the first line that is no UTF-8, and io.read_bytes with bytes.to_string_lossy reads the whole file at once and writes U+FFFD in such a line. To read a large file a piece at a time and have each line that is no UTF-8 as an Err of its own, read it with stream.file_chunks, split each piece at \n and keep its last part, the start of a line the next piece goes on with. A line keeps the \r of a \r\n line end.

import bytes
import io
import list
import option
import stream

-- Calls f with each line of the file at path, as it reads the file 64 KiB
-- at a time: Ok(text), or Err(why) for a line that is no UTF-8.
fn each_line(
  path: String,
  f: Fn(Result(String, bytes.BytesError)) -> (),
) -> Result((), io.IoError) {
  let nl = bytes.from_string("\n")
  let rest = stream.file_chunks(path, 65536)
    |> stream.fold(Ok(bytes.empty())) { carry, chunk ->
      match (carry, chunk) {
        (Ok(before), Ok(b)) -> {
          -- the last part is a line the next chunk goes on with
          let parts = bytes.split(bytes.concat(before, b), nl)
          list.each(list.take(parts, list.length(parts) - 1)) { line -> f(bytes.to_string(line)) }
          Ok(list.last(parts) |> option.unwrap_or(bytes.empty()))
        }
        (Err(e), _) -> Err(e)
        (_, Err(e)) -> Err(e)
      }
    }
  match rest {
    Ok(last) when bytes.is_empty(last) -> Ok(())
    Ok(last) -> Ok(f(bytes.to_string(last)))
    Err(e) -> Err(e)
  }
}

fn main() {
  -- "one\nbad \xff\ntwo\n"
  when let Ok(raw) = bytes.from_hex("6f6e650a62616420ff0a74776f0a") else {
    panic("no hex")
  }
  let _ = io.write_bytes("mixed.log", raw)
  let read = each_line("mixed.log") { line ->
    match line {
      Ok(text) -> println(text)
      Err(e) -> println("skipped: {e}")
    }
  }
  println(read) -- Ok(())
}
open in playground

Design notes

  • Streams are channels. No new value type. stream.collect(ch) works on any Channel, not just streams produced by this module.
  • A stage takes what its reader asks for. A transform takes a value from its input only when its reader (a sink, the stage after it, or a read such as channel.receive) asks for one, and calls its function only on that value: ch |> stream.map(f) |> stream.take(2) takes two values from ch and runs f twice, and a stage whose output nobody reads takes nothing and computes nothing. A sink that reads to the end (collect, fold, length, last, write_to_file, write_to_tcp, channel.each) asks for every value: the stage before it works up to 16 values ahead while it reads, and so does each stage before that, up to a take, a take_while or a zip, which ask for one value at a time, as first, fold_until, channel.fold_until and a read by hand do. A read that does not wait (channel.try_receive, a channel.receive_timeout that runs out) asks too: the value it asked for comes to a later read, and is gone if the stream is closed or cut short before then (at most one value for each stage). A source does not wait to be asked: it works ahead of its reader until 16 values wait in its output (file_lines reads up to 64 KiB of lines at a time). stream.buffered(n) is the only transform that reads ahead for any reader, up to n values.
  • A failing stage fails the pipeline. If the function of a stage raises a runtime error (a division by zero, a panic), the stage closes its output with that failure. The next stage fails with it in turn, and the sink at the end raises it: stream.collect does not return the values that came before the failure. Every read of a stream raises it too: channel.each, channel.receive, channel.try_receive, channel.receive_timeout and a Recv arm of channel.select. A stage can take a value and fail on it before anybody reads its output: a stage that read ahead for stream.buffered, or one that a read which did not wait asked for a value. When the program lets go of the stream, that failure goes with it, unreported; a name is let go when the block that binds it ends (the body of its function, its { } block), so a program that ends normally says nothing of it. Only a program that ends while it still holds the stream (it fails, a deadlock stops it, a REPL session ends) reports it: “a stream.map stage failed and its output was never read”. A failure that a read has raised is not reported again, a stage that nobody asked for a value computes nothing and so does not fail, and a pipeline that is cut short stays quiet.
  • A source of your own is stream.from_fn. stream.from_fn(feed) runs feed as the stream’s source: what it sends on the channel it is given is the stream, which closes when feed returns (or closes the channel itself). A runtime error in feed fails the stream as a failing stage does, and when nobody reads the stream any more (it is cut short, closed or left), feed is stopped where it is.
  • A pipeline that is cut short stops. When a stage or a sink ends before its input does because the code says so (take, take_while, first, a Stop of fold_until), the stage that feeds it is stopped, and so on up to the source, before the call returns. So stream.repeat(x) |> stream.take(3) leaves nothing running, and a caller that reads the stream afterwards finds it cut. The output of a stage is closed by this: read it with one consumer. A channel that no stage feeds (one you made with channel.new) is left open, and what is left in it stays there: a stage took only what was asked for, so ch |> stream.map(f) |> stream.take(2) takes exactly two values from ch, and a filter stops where its reader stops. Two things take more. stream.buffered(n) took up to n values ahead, which are gone when the pipeline is cut short. And a merge, a zip or a channel.select over several streams asks each of them, so each stage that feeds it may hold one value for a later read; when nothing reads it any more, that value is gone: at most one per input.
  • A read that is stopped leaves its stream whole. A sink that is stopped before its end (its task is stopped, for example when its time under task.deadline runs out, or a call of its function fails) cuts nothing, and neither does a stage that is stopped, that fails, or whose readers let it go: the stream is its caller’s, and stays whole, less what was taken (what the sink took, and the value that a stage between them was on), for whoever still has it. So it is the same whether the stream is read directly or through stages: channel.each(s |> stream.map(f), g) leaves s whole as channel.each(s, g) does. A read that is stopped asks for nothing more: the stages of the stream go back to waiting for a reader, and compute nothing for the read that is over (at most the value each was on). A stream that nobody else has is let go of with the read, and stops. channel.each, channel.fold_until and the workers of a task.each leave a stream they read in the same way.
  • A TCP source holds its connection as any other name does. stream.tcp_lines(conn) and stream.tcp_chunks(conn, n) keep the connection while their stream is read. When the stream is stopped (left, closed with channel.close, cut short by stream.first or stream.take), the source stops reading and lets go of the connection; it does not shut it. (A read of the source that times out, at SILT_IO_TIMEOUT, shuts it, as every tcp operation that times out does.) The connection is closed when its last holder is gone, as every value is: a handler that reads the head of a request through stream.tcp_lines(conn) in a helper can answer on conn afterwards, and one that reads lines and returns, holding conn no more, closes it (the client sees the end). tcp.close(conn) shuts a connection whoever else holds it. The source reads ahead: what it had read and nobody took (the rest of the lines of its last read, up to 64 KiB) is gone with the stream, and a later tcp.read(conn, ..) gets what comes after it. A read the source had in flight ends at once without taking anything, on a plain connection and on a TLS one alike; on a TLS connection the source waits for the peer’s bytes without holding the connection, so a write on it does not wait for the source. (A source has a descriptor of its own for this, which it gives back when its stream is gone. A process that has none left gets a stream whose one item is the error.)
  • A TCP sink that is stopped closes its connection. When the task in stream.write_to_tcp is stopped, the connection is shut down, as by tcp.close: a write that may be in flight has to end, and the peer must not take a part of a write for the whole.
  • A read or a write of a source or a sink waits for I/O, and SILT_IO_TIMEOUT ends it as it ends any wait for I/O (see I/O timeouts): a source ends with its module’s timeout error as its last item (Err(Timeout) of a connection, which is shut down then, as after any tcp operation that times out), a sink returns it. So does a read or a write that the I/O pool could not run: the source ends with that error, not as if its input had ended.
  • A stream that you stop reading stops taking. A stream that is read by hand (channel.receive, channel.select) and then left takes nothing more from what feeds it: its transforms wait for a reader. Its source and a stream.buffered work on until their outputs are full or the source is at its end. channel.close(s) on the stream stops them at once.
  • A stream that nothing can read any more is closed. When the last name of a stream goes (the function that read it returned, the loop went round to its next stream), it is closed as channel.close(s) closes it: its stages stop, a TCP source lets go of its connection (above), and the tasks of the stages are gone. So a handler that reads a few lines of stream.tcp_lines(conn) and returns leaves nothing behind (the connection, held by nothing else, closes: the client sees the end), and neither does a loop that reads one value of each of a million streams. A stream that a later stage reads, or that a channel or a task holds, is not left.
  • Errors flow through the stream. File sources emit Channel(Result(_, IoError)); TCP sources emit Channel(Result(_, TcpError)). Each chunk can fail independently; consumers pattern-match. A file’s error names its path, as io’s do (file not found: app.log). A line that is not UTF-8 is an error of file_lines and tcp_lines alike, and their last item: the source ends there, and tcp_lines shuts its connection down. file_lines names the line and the path (InvalidInput("line 3 of app.log is not UTF-8")). No byte is replaced: a program that wants the other lines of such a file reads it with io.read_bytes and decodes it with bytes.to_string_lossy, which writes U+FFFD for what is no UTF-8, or reads it a piece at a time with stream.file_chunks, each such line an Err of its own (see the lines of a file that has lines that are no UTF-8). To change only the Ok values and pass each Err(_) through, map with result.map: ch |> stream.map { r -> result.map(r, f) }.
  • No async/await. Stages are tasks of the silt scheduler; file and socket reads run on the I/O pool while the stage waits.
  • stream.repeat is infinite. Always pair it with take, take_while, or another bounded sink — collect on an unbounded stream will hang.
  • A channel is a Functor. Functor.map of a Channel(_) is stream.map (see Functors), so generic code bounded by Functor starts a stage when it is given a channel. A channel has no Applicative or Monad.

A function over any Functor, given a stream:

import stream

fn double_all(xs: m(Int)) -> m(Int) where m: Functor {
  Functor.map(xs) { x -> x * 2 }
}

fn main() {
  let doubled = stream.from_list([1, 2, 3]) |> double_all |> stream.collect
  println(doubled) -- [2, 4, 6]
}
open in playground

stream.from_list

stream.from_list(xs: List(a)) -> Channel(a)

A stream of the elements of xs, closed after the last.

import stream

fn main() {
  println(stream.from_list(["a", "b"]) |> stream.collect) -- ["a", "b"]
}
open in playground

stream.from_range

stream.from_range(lo: Int, hi: Int) -> Channel(Int)

A stream of the numbers from lo to hi, both included, closed after the last.

import stream

fn main() {
  println(stream.from_range(1, 5) |> stream.collect) -- [1, 2, 3, 4, 5]
}
open in playground

stream.repeat

stream.repeat(x: a) -> Channel(a)

A stream of x without end: take a part of it.

import stream

fn main() {
  println(stream.repeat("hi") |> stream.take(3) |> stream.collect) -- ["hi", "hi", "hi"]
}
open in playground

stream.unfold

stream.unfold(seed: a, f: Fn(a) -> Option((b, a))) -> Channel(b)

A stream made from a seed: f gives Some((value, next_seed)) for each value, and None to close it.

import stream

fn main() {
  let powers = stream.unfold(1) { n ->
    match n > 100 {
      true -> None
      false -> Some((n, n * 2))
    }
  }
  println(stream.collect(powers)) -- [1, 2, 4, 8, 16, 32, 64]
}
open in playground

stream.from_fn

stream.from_fn(feed: Fn(Channel(a)) -> ()) -> Channel(a)

A stream of what feed sends on the channel it is given. feed runs in a task of its own as the stream’s source, and works ahead of its reader: a send of feed waits while 16 of its values wait to be read, or 32 under a sink that reads to the end (collect, fold, channel.each, …). A read that does not wait (channel.try_receive, a channel.receive_timeout that runs out) gets what feed has sent, and the end of the stream once feed has returned, as from any source; so does a channel.select, for which a Recv arm on the stream is ready then, beside its other ready arms. The stream closes when feed returns. A feed that closes its channel itself ends the stream there as returning does, and runs on until it returns or nobody reads the stream any more; a runtime error of feed after that close (a send on the closed channel) is dismissed, as every failure that comes after the end of a stream is. A runtime error in feed fails the stream as a failing stage does: the sink or the read that reads it raises the error. When nobody reads the stream any more (it is cut short, closed, or its last name is gone: a name goes when the block that binds it ends), feed is stopped, in a send or wherever it waits.

import channel
import list
import stream

fn main() {
  let squares = stream.from_fn { ch -> list.each([1, 2, 3]) { n -> channel.send(ch, n * n) } }
  println(stream.collect(squares)) -- [1, 4, 9]
  -- A feed without end, of which a reader takes two values.
  let naturals = stream.from_fn { ch ->
    let count = { n ->
      channel.send(ch, n)
      count(n + 1)
    }
    count(0)
  }
  println(naturals |> stream.take(2) |> stream.collect) -- [0, 1]
}
open in playground

stream.file_chunks

stream.file_chunks(path: String, size: Int) -> Channel(Result(Bytes, IoError))

The bytes of the file at path, in pieces of at most size bytes. A failure to open or read it is an Err element, and the stream ends there. A size below 1 is a runtime error, “stream.file_chunks: size must be positive”.

import bytes
import io
import stream

fn main() {
  let _ = io.write_file("data.bin", "0123456789")
  let sizes = stream.file_chunks("data.bin", 4)
    |> stream.map { chunk ->
      match chunk {
        Ok(b) -> bytes.length(b)
        Err(_) -> -1
      }
    }
  println(stream.collect(sizes)) -- [4, 4, 2]
}
open in playground

stream.file_lines

stream.file_lines(path: String) -> Channel(Result(String, IoError))

The lines of the file at path, without their line ends, as UTF-8 text. A failure to open or read it, or text that is no UTF-8, is an Err element, and the stream ends there.

import io
import stream

fn main() {
  let _ = io.write_file("notes.txt", "one\ntwo\nthree\n")
  println(stream.file_lines("notes.txt") |> stream.collect) -- [Ok("one"), Ok("two"), Ok("three")]
}
open in playground

stream.stdin_lines

stream.stdin_lines() -> Channel(Result(String, IoError))

The lines of stdin, without their line ends, until it ends. See The lines of stdin.

import stream

fn main() {
  let n = stream.stdin_lines() |> stream.length
  println("{n} lines")
}
open in playground

stream.tcp_chunks

stream.tcp_chunks(stream: TcpStream, size: Int) -> Channel(Result(Bytes, TcpError))

The bytes that come on a TCP connection, in pieces of at most size bytes, until the other end closes it. A failure is an Err element, and the stream ends there.

import bytes
import stream
import tcp

fn main() -> Result((), tcp.TcpError) {
  let conn = tcp.connect("127.0.0.1:7000")?
  let total = stream.tcp_chunks(conn, 4096)
    |> stream.fold(0) { n, chunk ->
      match chunk {
        Ok(b) -> n + bytes.length(b)
        Err(_) -> n
      }
    }
  println("{total} bytes")
  Ok(())
}
open in playground

stream.tcp_lines

stream.tcp_lines(stream: TcpStream) -> Channel(Result(String, TcpError))

The lines that come on a TCP connection, without their line ends, until the other end closes it. A failure, or text that is no UTF-8, is an Err element, and the stream ends there.

import list
import stream
import tcp

fn main() -> Result((), tcp.TcpError) {
  let conn = tcp.connect("127.0.0.1:7000")?
  stream.tcp_lines(conn) |> stream.take(3) |> stream.collect |> list.each { line -> println(line) }
  Ok(())
}
open in playground

stream.map

stream.map(ch: Channel(a), f: Fn(a) -> b) -> Channel(b)

Each value of ch passed through f.

import stream

fn main() {
  println(stream.from_list([1, 2, 3]) |> stream.map { n -> n * 10 } |> stream.collect) -- [10, 20, 30]
}
open in playground

stream.filter

stream.filter(ch: Channel(a), pred: Fn(a) -> Bool) -> Channel(a)

The values of ch for which pred is true.

import stream

fn main() {
  println(stream.from_range(1, 10) |> stream.filter { n -> n % 3 == 0 } |> stream.collect) -- [3, 6, 9]
}
open in playground

stream.flat_map

stream.flat_map(ch: Channel(a), f: Fn(a) -> List(b)) -> Channel(b)

The elements of the lists f gives for each value of ch, in order.

import stream

fn main() {
  println(stream.from_list([1, 2]) |> stream.flat_map { n -> [n, n] } |> stream.collect) -- [1, 1, 2, 2]
}
open in playground

stream.take

stream.take(ch: Channel(a), n: Int) -> Channel(a)

The first n values of ch. It stops reading ch after them.

import stream

fn main() {
  println(stream.from_range(1, 1000) |> stream.take(3) |> stream.collect) -- [1, 2, 3]
}
open in playground

stream.drop

stream.drop(ch: Channel(a), n: Int) -> Channel(a)

The values of ch after the first n.

import stream

fn main() {
  println(stream.from_range(1, 5) |> stream.drop(3) |> stream.collect) -- [4, 5]
}
open in playground

stream.take_while

stream.take_while(ch: Channel(a), pred: Fn(a) -> Bool) -> Channel(a)

The values of ch up to the first for which pred is false, which is not passed on.

import stream

fn main() {
  println(stream.from_list([1, 2, 5, 1]) |> stream.take_while { n -> n < 3 } |> stream.collect) -- [1, 2]
}
open in playground

stream.drop_while

stream.drop_while(ch: Channel(a), pred: Fn(a) -> Bool) -> Channel(a)

The values of ch from the first for which pred is false on.

import stream

fn main() {
  println(stream.from_list([1, 2, 5, 1]) |> stream.drop_while { n -> n < 3 } |> stream.collect) -- [5, 1]
}
open in playground

stream.chunks

stream.chunks(ch: Channel(a), n: Int) -> Channel(List(a))

The values of ch in lists of n; the last list holds what is left. An n below 1 is a runtime error, “stream.chunks: n must be positive”.

import stream

fn main() {
  println(stream.from_range(1, 7) |> stream.chunks(3) |> stream.collect) -- [[1, 2, 3], [4, 5, 6], [7]]
}
open in playground

stream.scan

stream.scan(ch: Channel(a), init: b, f: Fn(b, a) -> b) -> Channel(b)

Each step of a fold: f(acc, value) for every value, starting from init. The initial value itself is not passed on.

import stream

fn main() {
  println(stream.from_list([1, 2, 3, 4]) |> stream.scan(0) { acc, n -> acc + n } |> stream.collect) -- [1, 3, 6, 10]
}
open in playground

stream.enumerate

stream.enumerate(ch: Channel(a)) -> Channel((Int, a))

Each value of ch with its index, counting from 0.

import stream

fn main() {
  println(stream.from_list(["a", "b"]) |> stream.enumerate |> stream.collect) -- [(0, "a"), (1, "b")]
}
open in playground

stream.dedup

stream.dedup(ch: Channel(a)) -> Channel(a) where a: Equal

The values of ch without a value equal to the one before it.

import stream

fn main() {
  println(stream.from_list([1, 1, 2, 2, 1]) |> stream.dedup |> stream.collect) -- [1, 2, 1]
}
open in playground

stream.buffered

stream.buffered(ch: Channel(a), n: Int) -> Channel(a)

Every value of ch, read up to n values ahead of the reader, so the stages before it can work while the reader is busy with an earlier value.

import stream

fn main() {
  println(stream.from_range(1, 3) |> stream.buffered(16) |> stream.collect) -- [1, 2, 3]
}
open in playground

stream.merge

stream.merge(chs: List(Channel(a))) -> Channel(a)

The values of all of chs, in the order they come; it closes when all are closed.

import list
import stream

fn main() {
  let all = stream.merge([stream.from_list([1, 2]), stream.from_list([3])]) |> stream.collect
  println(list.sort(all)) -- [1, 2, 3]
}
open in playground

stream.concat

stream.concat(chs: List(Channel(a))) -> Channel(a)

The values of each stream of chs in turn: all of the first, then all of the second, and so on.

import stream

fn main() {
  println(stream.concat([stream.from_list([1, 2]), stream.from_list([3])]) |> stream.collect) -- [1, 2, 3]
}
open in playground

stream.zip

stream.zip(a: Channel(a), b: Channel(b)) -> Channel((a, b))

Pairs of a value of a and a value of b, in order; it closes when either closes.

import stream

fn main() {
  println(stream.zip(stream.from_list(["a", "b", "c"]), stream.from_range(1, 2)) |> stream.collect) -- [("a", 1), ("b", 2)]
}
open in playground

stream.collect

stream.collect(ch: Channel(a)) -> List(a)

Reads ch to its end and gives its values as a list.

import stream

fn main() {
  println(stream.from_range(1, 3) |> stream.collect) -- [1, 2, 3]
}
open in playground

stream.fold

stream.fold(ch: Channel(a), init: b, f: Fn(b, a) -> b) -> b

Reads ch to its end and gives f(acc, value) over its values, starting from init.

import stream

fn main() {
  println(stream.from_range(1, 10) |> stream.fold(0) { acc, n -> acc + n }) -- 55
}
open in playground

stream.fold_until

stream.fold_until(ch: Channel(a), init: b, f: Fn(b, a) -> Step(b)) -> b

Folds the values of ch into a state, starting from init, as list.fold_until folds a list: f gives Continue(next) to go on or Stop(value) to end, and the call gives that value, or the last state when the stream ends. It asks for one value at a time, as first does, and a Stop cuts the stream short: the stages that feed it are stopped, as when stream.take has its values. A stage that fails makes the fold raise its failure.

import stream

fn main() {
  -- The sum of the values before the first one above 3.
  let total = stream.from_range(1, 1000000)
    |> stream.fold_until(0) { acc, n ->
      match n > 3 {
        true -> Stop(acc)
        false -> Continue(acc + n)
      }
    }
  println(total) -- 6
}
open in playground

stream.length

stream.length(ch: Channel(a)) -> Int

Reads ch to its end and gives the number of values.

import stream

fn main() {
  println(stream.from_range(1, 10) |> stream.filter { n -> n % 2 == 0 } |> stream.length) -- 5
}
open in playground

stream.first

stream.first(ch: Channel(a)) -> Option(a)

The first value of ch, or None when it is closed without one. It reads no more of ch.

import stream

fn main() {
  println(stream.from_range(5, 9) |> stream.first) -- Some(5)
  println(stream.from_list([1]) |> stream.drop(1) |> stream.first) -- None
}
open in playground

stream.last

stream.last(ch: Channel(a)) -> Option(a)

Reads ch to its end and gives its last value, or None when it had none.

import stream

fn main() {
  println(stream.from_range(5, 9) |> stream.last) -- Some(9)
}
open in playground

stream.write_to_file

stream.write_to_file(ch: Channel(Bytes), path: String) -> Result((), IoError)

Writes the bytes of ch to the file at path, which it creates or empties, until ch closes. A failure to write is an Err: into a folder that does not exist, Err(FolderNotFound(folder)), as for io.write_file.

import bytes
import io
import stream

fn main() {
  let lines = stream.from_list(["a", "b"]) |> stream.map { s -> bytes.from_string("{s}\n") }
  println(stream.write_to_file(lines, "out.txt")) -- Ok(())
  println(io.read_file("out.txt")) -- Ok("a\nb\n")
}
open in playground

stream.write_to_tcp

stream.write_to_tcp(ch: Channel(Bytes), stream: TcpStream) -> Result((), TcpError)

Writes the bytes of ch to a TCP connection until ch closes. A failure to write is an Err.

import bytes
import stream
import tcp

fn main() -> Result((), tcp.TcpError) {
  let conn = tcp.connect("127.0.0.1:7000")?
  let lines = stream.from_list(["one", "two"]) |> stream.map { s -> bytes.from_string("{s}\n") }
  stream.write_to_tcp(lines, conn)?
  tcp.close(conn)
  Ok(())
}
open in playground