From debdc8fc31786cb68d95965f790f9e2ddba10ecc Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Wed, 14 Jun 2023 11:38:01 -0400 Subject: [PATCH] add `Fut.for_array` to easily iterate on an array in parallel --- src/fut.ml | 3 +++ src/fut.mli | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/src/fut.ml b/src/fut.ml index 0a16dcce..7c2d199d 100644 --- a/src/fut.ml +++ b/src/fut.ml @@ -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 = diff --git a/src/fut.mli b/src/fut.mli index a0b10685..16cf1979 100644 --- a/src/fut.mli +++ b/src/fut.mli @@ -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