diff --git a/core/CCKList.ml b/core/CCKList.ml index 95809eaf..f59ea6ad 100644 --- a/core/CCKList.ml +++ b/core/CCKList.ml @@ -34,6 +34,8 @@ type + 'a t = let nil = `Nil let cons a b = `Cons (a,b) +let singleton x = `Cons (x, fun () -> `Nil) + let to_list l = let rec direct i (l:'a t) = match l with | `Nil -> [] @@ -45,6 +47,12 @@ let to_list l = in direct 200 l +let of_list l = + let rec aux l () = match l with + | [] -> `Nil + | x::l' -> `Cons (x, aux l') + in aux l () + type 'a sequence = ('a -> unit) -> unit type 'a gen = unit -> 'a option @@ -65,6 +73,10 @@ let rec fold f acc res = match res with | `Nil -> acc | `Cons (s, cont) -> fold f (f acc s) (cont ()) +let rec iter f l = match l with + | `Nil -> () + | `Cons (x, l') -> f x; iter f (l' ()) + let length l = fold (fun acc _ -> acc+1) 0 l let rec take n (l:'a t):'a t = match l with diff --git a/core/CCKList.mli b/core/CCKList.mli index 7ed21cd2..cb568e27 100644 --- a/core/CCKList.mli +++ b/core/CCKList.mli @@ -34,8 +34,12 @@ val nil : 'a t val cons : 'a -> (unit -> 'a t) -> 'a t +val singleton : 'a -> 'a t + +val of_list : 'a list -> 'a t + val to_list : 'a t -> 'a list - (** Gather all values into a list *) +(** Gather all values into a list *) type 'a sequence = ('a -> unit) -> unit type 'a gen = unit -> 'a option @@ -44,7 +48,9 @@ val to_seq : 'a t -> 'a sequence val to_gen : 'a t -> 'a gen val fold : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a - (** Fold on values *) +(** Fold on values *) + +val iter : ('a -> unit) -> 'a t -> unit val length : 'a t -> int diff --git a/examples/mem_size.ml b/examples/mem_size.ml index e89fb83c..d424e9ed 100644 --- a/examples/mem_size.ml +++ b/examples/mem_size.ml @@ -1,7 +1,7 @@ (** Compute the memory footprint of a value (and its subvalues). Reference is http://rwmj.wordpress.com/2009/08/05/ocaml-internals-part-2-strings-and-other-types/ *) -open Sequence.Infix +module Sequence = CCSequence (** A graph vertex is an Obj.t value *) let graph =