add Fut.for_array to easily iterate on an array in parallel

This commit is contained in:
Simon Cruanes 2023-06-14 11:38:01 -04:00
parent 826d1f15c8
commit debdc8fc31
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 10 additions and 0 deletions

View file

@ -303,6 +303,9 @@ let for_ ~on n f : unit t =
~map:(fun _f () -> ())
()
let for_array ~on arr f : unit t =
for_ ~on (Array.length arr) (fun i -> f i arr.(i))
(* ### blocking ### *)
let wait_block (self : 'a t) : 'a or_error =

View file

@ -130,6 +130,13 @@ val for_ : on:Pool.t -> int -> (int -> unit) -> unit t
a future that resolves when all the tasks have resolved, or fails
as soon as one task has failed. *)
val for_array : on:Pool.t -> 'a array -> (int -> 'a -> unit) -> unit t
(** [for_array ~on arr f] runs [f 0 arr.(0)], …, [f (n-1) arr.(n-1)] in
the pool (where [n = Array.length arr]), and returns a future
that resolves when all the tasks are done,
or fails if any of them fails.
@since 0.2 *)
(** {2 Blocking} *)
val wait_block : 'a t -> 'a or_error