gens

Package Version Hex Docs

Gleam generators and lazy infinite lists!

gleam add gens

LazyList

import gleam/int
import gens

pub fn main() -> Nil {
  gens.new()
  |> gens.map(fn(x) { x + 3 })
  |> gens.filter(fn(x) {x % 2 != 0 })
  |> gens.map(int.to_string)
  |> take(5)
  |> echo
  // -> ["3", "5", "7", "9", "11"]
  Nil
}

Generator

import gens
import gleam/option

pub fn main() -> Nil {
  let counter =
    gens.Generator(state: 0, next: fn(c) { option.Some(#(c, c + 1)) })

  let #(nums, counter2) = gens.gen(counter, 5)
  echo nums
  // -> [0, 1, 2, 3, 4]
  echo counter2.state
  // -> 5
  Nil
}

Search Document