Prelude.GenTrivial generators ("lazy" sequences / streams). NOT thread-safe.
An 'a t generator is just a thunk that, called repeatedly, returns (Some 'a) for each element of the sequence it represents until the sequence is exhausted, when it returns None.
Example:
Gen.(catch readline >> to_list) = readlinesTerminology:
Gen.fold) is one that calls its generator repeatedly until the generator returns Nonemap) is one that returns a new generator without calling the original generator at all (or at least, without exhausting all of it)val catch : ?this:exn -> ('b -> 'a) -> 'b -> 'a t(catch f x) creates a generator from a function f that, after repeated application to x, eventually raises an exception.
Example: (catch input_line chan) is a generator of the lines on chan.
Any exception will indicate the end of the sequence; if you want more precision, specify the exception with ~this -- in this case, any other exception will be re-raised: e.g. (catch ~this:End_of_file input_line chan)
val optional : ('a -> 'b option) -> 'a -> 'b t(optional f x) creates a generator from a function f that, after repeated application to x, eventually returns None.
(cons x g) is the the generator g with x at its head.
(cons 3 empty |> cons 2 |> cons 1 |> to_list) = [1;2;3] val singleton : 'a -> 'a t(singleton x) is the generator producing x and only x.
(append g1 g2) is the generator that produces all the items in g1 followed by those in g2.
val of_list : 'a list -> 'a t(to_list list) is the generator that produces all the elements of list.
(of_list [1;2;3] |> to_list) = [1;2;3] (chars_of_string str) is the generator that produces all the characters of str.
val lines : Stdlib.in_channel -> string t(lines chan) is the generator that produces all the lines on the input channel chan.
(random ~size elt ()) is a generator of (size ()) (default: > 100) random elements; each element is the result of a call to (elt ()).
val random : size:(unit -> int) -> (unit -> 'a) -> unit -> 'a t(random ~size elt ()) is a generator of (size ()) (default: > 100) random elements; each element is the result of a call to (elt ()).
val fold : ('a -> 'b -> 'b) -> 'b -> 'a t -> 'b(fold f acc g) folds f across all the values of the generator g; exhaustive.
val len : 'a t -> int(len g) is the length of the generator g; exhaustive.
val iter : ('a -> unit) -> 'a t -> unit(iter f g) iterates f across all the values of the generator g; exhaustive.
val to_list : 'a t -> 'a list(to_list g) returns the list of all the values of the generator g; exhaustive.
val to_string : char t -> string(to_string g) returns the string of all the characters of the generator g; exhaustive.
val map : ('a -> 'b) -> 'a t -> unit -> 'b option(map f g) maps f across all the value of the generator g; lazy.
val mapi : (int -> 'a -> 'b) -> 'a t -> unit -> 'b option(mapi f g) maps (fun x -> f i x) across all the values of the generator g, where i is the 0-based index of each value; lazy.
(take n g) is the generator consisting of the first n elements of g; lazy.
(drop n g) is the generator consisting of all the elements of g, excluding the first n; lazy.
(splitwhile p g) is (a,b) where a is the generator that produces the longest prefix of g of elements that satisfy p and b is the generator that produces the remaining suffix.
Exhaustive in the prefix, lazy in the suffix.
(takewhile p g) is the generator that produces (only) the leading elements of g for which p is true.