Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add CCArray.of_iter #472

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/core/CCArray.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 () ->
Expand Down
5 changes: 5 additions & 0 deletions src/core/CCArray.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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 *)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I normally use @since NEXT_RELEASE :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks


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
Expand Down
5 changes: 5 additions & 0 deletions src/core/CCArrayLabels.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions tests/core/t_array.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)