gs

Types

A stream of values of type a.

pub type Stream(a) {
  Stream(pull: fn() -> Option(#(a, Stream(a))))
}

Constructors

  • Stream(pull: fn() -> Option(#(a, Stream(a))))

Functions

pub fn chunks(stream: Stream(a), size: Int) -> Stream(List(a))

Creates a stream that emits chunks of a given size.

Example:

repeat(1) |> take(10) |> chunks(3)
pub fn concat(first: Stream(a), second: Stream(a)) -> Stream(a)

Concatenates two streams.

Example:

pure(1) |> concat(pure(2))
pub fn debug(stream: Stream(a)) -> Stream(a)

Logs each element of a stream for debugging.

Example:

pure(42) |> debug
pub fn drop(stream: Stream(a), n: Int) -> Stream(a)

Drops the first n elements from a stream.

Example:

repeat(1) |> drop(5)
pub fn filter(
  stream: Stream(a),
  pred: fn(a) -> Bool,
) -> Stream(a)

Filters a stream based on a predicate.

Example:

repeat(1) |> filter(fn(x) { x > 0 })
pub fn flat_map(
  stream: Stream(a),
  f: fn(a) -> Stream(b),
) -> Stream(b)

Flat maps a function over a stream.

Example:

repeat(1) |> flat_map(fn(x) { pure(x + 1) })
pub fn from_counter(start: Int) -> Stream(Int)

Creates a stream that counts up from a given number.

Example:

let stream = from_counter(1)
pub fn from_empty() -> Stream(a)

Creates an empty stream.

Example:

let stream = from_empty()
pub fn from_list(items: List(a)) -> Stream(a)

Creates a stream from a list of items.

Example:

[1, 2, 3] |> from_list
pub fn from_option(option: Option(a)) -> Stream(a)

Creates a stream from an option.

Example:

Some(42) |> from_option
pub fn from_pure(value: a) -> Stream(a)

Creates a stream with a single value.

Example:

let stream = pure(42)
pub fn from_range(start: Int, end: Int) -> Stream(Int)

Creates a stream that counts up from a given number, including the end.

Example:

let stream = from_range(1, 5)
pub fn from_range_exclusive(start: Int, end: Int) -> Stream(Int)

Creates a stream that counts up from a given number, excluding the end.

Example:

let stream = from_range_exclusive(1, 5)
pub fn from_repeat(value: a) -> Stream(a)

Repeats a value indefinitely in a stream.

Example:

42 |> repeat
pub fn from_repeat_eval(f: fn() -> a) -> Stream(a)

Repeats the result of a function indefinitely in a stream.

Example:

fn() { 42 } |> repeat_eval
pub fn from_result(result: Result(a, b)) -> Stream(a)

Creates a stream from a result.

Example:

Ok(42) |> from_result
pub fn from_subject(subject: Subject(a)) -> Stream(a)
pub fn from_subject_timeout(
  subject: Subject(a),
  timeout_ms: Int,
) -> Stream(a)
pub fn from_tick(delay_ms: Int) -> Stream(Int)

Creates a stream from a tick in ms.

Example:

from_tick(1000)
pub fn from_timestamp_eval() -> Stream(Int)

Creates a stream that emits the current timestamp.

Example:

const stream = from_timestamp()
pub fn intersperse(stream: Stream(a), separator: a) -> Stream(a)

Interleaves a separator between each element of a stream.

Example:

repeat(1) |> intersperse(0)
pub fn map(stream: Stream(a), f: fn(a) -> b) -> Stream(b)

Maps a function over a stream.

Example:

repeat(1) |> map(fn(x) { x + 1 })
pub fn println(stream: Stream(String)) -> Stream(String)

Prints each element of a stream to the console.

Example:

pure("Hello, world!") |> println
pub fn recover(
  stream: Stream(Result(a, b)),
  recover: fn(b) -> Stream(a),
) -> Stream(a)

Recovers from an error in a stream using the given function. Alias for try_recover.

pub fn sleep(stream: Stream(a), delay_ms: Int) -> Stream(a)

Sleeps for a given number of milliseconds before pulling the next value from a stream.

Example:

repeat(1) |> sleep(1000)
pub fn take(stream: Stream(a), n: Int) -> Stream(a)

Takes the first n elements from a stream.

Example:

repeat(1) |> take(5)
pub fn take_while(
  stream: Stream(a),
  pred: fn(a) -> Bool,
) -> Stream(a)

Takes elements from a stream while a predicate is true.

Example:

repeat(1) |> take_while(fn(x) { x < 5 })
pub fn tap(stream: Stream(a), f: fn(a) -> b) -> Stream(a)

Applies a function to each element of a stream for side effects.

Example:

repeat(1) |> tap(fn(x) { io.println(x) })
pub fn to_fold(
  stream: Stream(a),
  initial: b,
  f: fn(b, a) -> b,
) -> b

Folds a stream into a single value.

Example:

repeat(1) |> take(5) |> to_fold(0, fn(acc, x) { acc + x })
pub fn to_list(stream: Stream(a)) -> List(a)

Collects a stream into a list.

Example:

repeat(1) |> take(5) |> to_list
pub fn to_nil(stream: Stream(a)) -> Nil

Collects a stream into a Nothing.

Example:

repeat(1) |> take(5) |> to_nil
pub fn to_option(stream: Stream(a)) -> Option(a)

Collects the first element of a stream into an option.

Example:

repeat(1) |> take(5) |> to_option
pub fn try_recover(
  stream: Stream(Result(a, b)),
  recover: fn(b) -> Stream(a),
) -> Stream(a)

Attempts to recover from an error in a stream using the given function. If the stream is successful, it remains unchanged. If the stream contains an error, the recovery function is used to attempt to create a new stream.

Examples

[1, 2, 3]
|> from_list
|> try_recover(fn(_) { pure(0) })
// -> Stream containing [1, 2, 3]
Error(5) 
|> from_result
|> try_recover(fn(error) { pure(error + 1) })
// -> Stream containing [6]
pub fn zip(left: Stream(a), right: Stream(b)) -> Stream(#(a, b))

Zips two streams together.

Example:

pure(1) |> zip(pure(2))
pub fn zip_all(
  left: Stream(a),
  right: Stream(b),
) -> Stream(Option(#(Option(a), Option(b))))

Zips two streams together, filling with None when one stream ends.

Example:

pure(1) |> zip_all(empty())
pub fn zip_all_with(
  left: Stream(a),
  right: Stream(b),
  f: fn(Option(a), Option(b)) -> c,
) -> Stream(c)

Zips two streams together with a function, filling with None when one stream ends.

Example:

pure(1) |> zip_all_with(empty(), fn(x, y) { #(x, y) })
pub fn zip_with(
  left: Stream(a),
  right: Stream(b),
  f: fn(a, b) -> c,
) -> Stream(c)

Zips two streams together with a function.

Example:

pure(1) |> zip_with(pure(2), fn(x, y) { x + y })
Search Document