additional functions for KList

This commit is contained in:
Simon Cruanes 2014-05-20 19:50:46 +02:00
parent ab83ea4827
commit c5b2373c03
3 changed files with 21 additions and 3 deletions

View file

@ -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

View file

@ -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

View file

@ -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 =