add CCKList.{of_array,to_array}

This commit is contained in:
Simon Cruanes 2015-08-31 13:42:13 +02:00
parent 6bbe443d85
commit 5a4d25b939
2 changed files with 35 additions and 0 deletions

View file

@ -334,6 +334,33 @@ let of_list l =
| x::l' -> `Cons (x, aux l') | x::l' -> `Cons (x, aux l')
in aux l in aux l
let of_array a =
let rec aux a i () =
if i=Array.length a then `Nil
else `Cons (a.(i), aux a (i+1))
in
aux a 0
let to_array l =
match l() with
| `Nil -> [| |]
| `Cons (x, _) ->
let n = length l in
let a = Array.make n x in (* need first elem to create [a] *)
iteri
(fun i x -> a.(i) <- x)
l;
a
(*$Q
Q.(array int) (fun a -> of_array a |> to_array = a)
*)
(*$T
of_array [| 1; 2; 3 |] |> to_list = [1;2;3]
of_list [1;2;3] |> to_array = [| 1; 2; 3; |]
*)
let rec to_seq res k = match res () with let rec to_seq res k = match res () with
| `Nil -> () | `Nil -> ()
| `Cons (s, f) -> k s; to_seq f k | `Cons (s, f) -> k s; to_seq f k

View file

@ -212,6 +212,14 @@ val of_list : 'a list -> 'a t
val to_list : 'a t -> 'a list val to_list : 'a t -> 'a list
(** Gather all values into a list *) (** Gather all values into a list *)
val of_array : 'a array -> 'a t
(** Iterate on the array
@since NEXT_RELEASE *)
val to_array : 'a t -> 'a array
(** Convert into array. Iterates twice.
@since NEXT_RELEASE *)
val to_rev_list : 'a t -> 'a list val to_rev_list : 'a t -> 'a list
(** Convert to a list, in reverse order. More efficient than {!to_list} *) (** Convert to a list, in reverse order. More efficient than {!to_list} *)