Skip to content

Commit 18ffdd7

Browse files
committed
add CCArray.of_iter
1 parent 31ad563 commit 18ffdd7

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

src/core/CCArray.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,12 @@ let to_seq a =
466466
467467
let to_iter a k = iter k a
468468
469+
let of_iter (i : 'a iter) : 'a array =
470+
let open CCVector in
471+
let vec = create () in
472+
i (push vec);
473+
to_array vec
474+
469475
let to_gen a =
470476
let k = ref 0 in
471477
fun () ->

src/core/CCArray.mli

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,11 @@ val to_iter : 'a t -> 'a iter
240240
in modification of the iterator.
241241
@since 2.8 *)
242242

243+
val of_iter : 'a iter -> 'a t
244+
(** [of_iter iter] builds a array from a given [iter].
245+
In the result, elements appear in the same order as they did in the source [iter].
246+
@since 3.15 *)
247+
243248
val to_seq : 'a t -> 'a Seq.t
244249
(** [to_seq a] returns a [Seq.t] of the elements of an array [a].
245250
The input array [a] is shared with the sequence and modification of it will result

src/core/CCArrayLabels.mli

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,11 @@ val to_iter : 'a t -> 'a iter
248248
in modification of the iterator.
249249
@since 2.8 *)
250250

251+
val of_iter : 'a iter -> 'a t
252+
(** [of_iter iter] builds a array from a given [iter].
253+
In the result, elements appear in the same order as they did in the source [iter].
254+
@since 3.15 *)
255+
251256
val to_seq : 'a t -> 'a Seq.t
252257
(** [to_seq a] returns a [Seq.t] of the elements of an array [a].
253258
The input array [a] is shared with the sequence and modification of it will result

tests/core/t_array.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,6 @@ q ~count:300 arr_arbitrary (fun a ->
309309
Array.sort CCInt.compare a1;
310310
sort_generic (module IA) ~cmp:CCInt.compare a2;
311311
a1 = a2)
312+
;;
313+
314+
q Q.(array int) (fun a -> of_iter (to_iter a) = a)

0 commit comments

Comments
 (0)