diff --git a/src/core/CCArray.ml b/src/core/CCArray.ml index 00c57142..f8904368 100644 --- a/src/core/CCArray.ml +++ b/src/core/CCArray.ml @@ -466,6 +466,12 @@ let to_seq a = let to_iter a k = iter k a +let of_iter (i : 'a iter) : 'a array = + let open CCVector in + let vec = create () in + i (push vec); + to_array vec + let to_gen a = let k = ref 0 in fun () -> diff --git a/src/core/CCArray.mli b/src/core/CCArray.mli index d19d4252..7f0a1ce9 100644 --- a/src/core/CCArray.mli +++ b/src/core/CCArray.mli @@ -240,6 +240,11 @@ val to_iter : 'a t -> 'a iter in modification of the iterator. @since 2.8 *) +val of_iter : 'a iter -> 'a t +(** [of_iter iter] builds a array from a given [iter]. + In the result, elements appear in the same order as they did in the source [iter]. + @since 3.15 *) + val to_seq : 'a t -> 'a Seq.t (** [to_seq a] returns a [Seq.t] of the elements of an array [a]. The input array [a] is shared with the sequence and modification of it will result diff --git a/src/core/CCArrayLabels.mli b/src/core/CCArrayLabels.mli index 8663c7dd..f681e482 100644 --- a/src/core/CCArrayLabels.mli +++ b/src/core/CCArrayLabels.mli @@ -248,6 +248,11 @@ val to_iter : 'a t -> 'a iter in modification of the iterator. @since 2.8 *) +val of_iter : 'a iter -> 'a t +(** [of_iter iter] builds a array from a given [iter]. + In the result, elements appear in the same order as they did in the source [iter]. + @since 3.15 *) + val to_seq : 'a t -> 'a Seq.t (** [to_seq a] returns a [Seq.t] of the elements of an array [a]. The input array [a] is shared with the sequence and modification of it will result diff --git a/tests/core/t_array.ml b/tests/core/t_array.ml index ecc76690..da836f05 100644 --- a/tests/core/t_array.ml +++ b/tests/core/t_array.ml @@ -309,3 +309,6 @@ q ~count:300 arr_arbitrary (fun a -> Array.sort CCInt.compare a1; sort_generic (module IA) ~cmp:CCInt.compare a2; a1 = a2) +;; + +q Q.(array int) (fun a -> of_iter (to_iter a) = a)