mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 03:05:28 -05:00
add CCKList.{of_array,to_array}
This commit is contained in:
parent
6bbe443d85
commit
5a4d25b939
2 changed files with 35 additions and 0 deletions
|
|
@ -334,6 +334,33 @@ let of_list l =
|
|||
| x::l' -> `Cons (x, 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
|
||||
| `Nil -> ()
|
||||
| `Cons (s, f) -> k s; to_seq f k
|
||||
|
|
|
|||
|
|
@ -212,6 +212,14 @@ val of_list : 'a list -> 'a t
|
|||
val to_list : 'a t -> '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
|
||||
(** Convert to a list, in reverse order. More efficient than {!to_list} *)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue