diff --git a/core/CCIO.ml b/core/CCIO.ml index cb688075..1e9313d1 100644 --- a/core/CCIO.ml +++ b/core/CCIO.ml @@ -363,6 +363,35 @@ module Seq = struct let of_fun g = g + let empty () = _stop() + + let singleton x = + let first = ref true in + fun () -> + if !first then (first := false; _yield x) else _stop() + + let cons x g = + let first = ref true in + fun () -> + if !first then (first := false; _yield x) else g() + + let of_list l = + let l = ref l in + fun () -> match !l with + | [] -> _stop() + | x::tail -> l:= tail; _yield x + + let of_array a = + let i = ref 0 in + fun () -> + if !i = Array.length a + then _stop() + else ( + let x = a.(!i) in + incr i; + _yield x + ) + (* TODO: wrapper around with_in? using bind ~finalize:... ? *) let chunks ~size ic = diff --git a/core/CCIO.mli b/core/CCIO.mli index 1d097617..58e24dea 100644 --- a/core/CCIO.mli +++ b/core/CCIO.mli @@ -248,6 +248,12 @@ module Seq : sig val of_fun : 'a gen -> 'a t (** Create a stream from a function that yields an element or stops *) + val empty : 'a t + val singleton : 'a -> 'a t + val cons : 'a -> 'a t -> 'a t + val of_list : 'a list -> 'a t + val of_array : 'a array -> 'a t + val chunks : size:int -> in_channel -> string t (** Read the channel's content into chunks of size [size] *)