mirror of
https://github.com/c-cube/moonpool.git
synced 2025-12-06 11:15:38 -05:00
feat: add Fut.for_list
This commit is contained in:
parent
d74c6da3fa
commit
b8588f2d65
3 changed files with 19 additions and 0 deletions
|
|
@ -318,6 +318,10 @@ let for_ ~on n f : unit t =
|
||||||
let for_array ~on arr f : unit t =
|
let for_array ~on arr f : unit t =
|
||||||
for_ ~on (Array.length arr) (fun i -> f i arr.(i))
|
for_ ~on (Array.length arr) (fun i -> f i arr.(i))
|
||||||
|
|
||||||
|
let for_list ~on l f : unit t =
|
||||||
|
let futs = List.rev_map (fun x -> spawn ~on (fun () -> f x)) l in
|
||||||
|
wait_list futs
|
||||||
|
|
||||||
(* ### blocking ### *)
|
(* ### blocking ### *)
|
||||||
|
|
||||||
let wait_block (self : 'a t) : 'a or_error =
|
let wait_block (self : 'a t) : 'a or_error =
|
||||||
|
|
|
||||||
|
|
@ -141,6 +141,10 @@ val for_array : on:Pool.t -> 'a array -> (int -> 'a -> unit) -> unit t
|
||||||
or fails if any of them fails.
|
or fails if any of them fails.
|
||||||
@since 0.2 *)
|
@since 0.2 *)
|
||||||
|
|
||||||
|
val for_list : on:Pool.t -> 'a list -> ('a -> unit) -> unit t
|
||||||
|
(** [for_list ~on l f] is like [for_array ~on (Array.of_list l) f].
|
||||||
|
@since 0.2 *)
|
||||||
|
|
||||||
(** {2 Blocking} *)
|
(** {2 Blocking} *)
|
||||||
|
|
||||||
val wait_block : 'a t -> 'a or_error
|
val wait_block : 'a t -> 'a or_error
|
||||||
|
|
|
||||||
|
|
@ -132,3 +132,14 @@ let () =
|
||||||
let () =
|
let () =
|
||||||
let x = [| Fut.return 1 |] |> Fut.join_array |> Fut.wait_block_exn in
|
let x = [| Fut.return 1 |] |> Fut.join_array |> Fut.wait_block_exn in
|
||||||
assert (x = [| 1 |])
|
assert (x = [| 1 |])
|
||||||
|
|
||||||
|
let () =
|
||||||
|
let run_for n =
|
||||||
|
let l = List.init n (fun x -> x) in
|
||||||
|
let sum = Atomic.make 0 in
|
||||||
|
Fut.for_list ~on:pool l (fun x -> ignore (Atomic.fetch_and_add sum x : int))
|
||||||
|
|> Fut.wait_block_exn;
|
||||||
|
assert (Atomic.get sum = List.fold_left ( + ) 0 l)
|
||||||
|
in
|
||||||
|
|
||||||
|
List.iter run_for [ 1; 10; 50; 1_000 ]
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue