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 empty() -> Stream(a)

Creates an empty stream.

Example:

let stream = empty()
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 fold(stream: Stream(a), initial: b, f: fn(b, a) -> b) -> b

Folds a stream into a single value.

Example:

repeat(1) |> take(5) |> fold(0, fn(acc, x) { acc + x })
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_result(result: Result(a, b)) -> Stream(a)

Creates a stream from a result.

Example:

Ok(42) |> from_result
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 pure(value: a) -> Stream(a)

Creates a stream with a single value.

Example:

let stream = pure(42)
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 repeat(value: a) -> Stream(a)

Repeats a value indefinitely in a stream.

Example:

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

Repeats the result of a function indefinitely in a stream.

Example:

fn() { 42 } |> repeat_eval
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 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_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