package oseq

  1. Overview
  2. Docs

Module OSeq.GeneratorSource

This interface is designed to make it easy to build complex streams of values in a way that resembles Python's generators (using "yield").

  let naturals : int OSeq.t = OSeq.Generator.(
      let rec aux n = yield n >>= fun () -> aux (n+1) in
      run (aux 0)
    )
  type 'a tree = E | N of 'a tree * 'a * 'a tree

  let traverse (t:'a tree) : 'a OSeq.t =
    let open OSeq.Generator in
    let rec trav = function
      | E -> empty
      | N (l,v,r) -> trav l >>= fun () -> yield v >>= fun () -> trav r
    in
    run (trav t)
Sourcetype 'a t

Type for writing generators (of type 'a OSeq.Generator.t) that can be used to construct an iterator of type 'a OSeq.t

Sourceval empty : 'a t

Empty generator, yields no value

Sourceval yield : 'a -> 'a t

Yield one value

Sourceval (>>=) : 'a t -> (unit -> 'a t) -> 'a t

gen1 >>= fun () -> gen2 first yields all values from gen1, and then all values from gen2

Sourceval delay : (unit -> 'a t) -> 'a t

Delayed generator, will evaluate the function when needed

Sourceval run : 'a t -> 'a seq

Iterator over the values yielded by the generator

OCaml

Innovation. Community. Security.