Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions lib_eio/core/eio__core.mli
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,11 @@ module Fiber : sig
invocations of [f] are run concurrently in separate fibers.
@param max_fibers Maximum number of fibers to run concurrently *)

val partition_map : ?max_fibers:int -> ('a -> ('b, 'c) Either.t) -> 'a list -> 'b list * 'c list
(** [partition_map f x] is like [List.partition_map f x] except that the
invocations of [f] are run concurrently in separate fibers.
@param max_fibers Maximum number of fibers to run concurrently *)

val iter : ?max_fibers:int -> ('a -> unit) -> 'a list -> unit
(** [iter f x] is like [List.iter f x] except that the invocations of [f] are
run concurrently in separate fibers.
Expand Down
1 change: 1 addition & 0 deletions lib_eio/core/fiber.ml
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ module List = struct
aux items

let map ?max_fibers fn = filter_map ?max_fibers (fun x -> Some (fn x))
let partition_map ?max_fibers fn items = List.partition_map Fun.id (map ?max_fibers fn items)
let filter ?max_fibers fn = filter_map ?max_fibers (fun x -> if fn x then Some x else None)

let iter ?(max_fibers=max_int) fn items =
Expand Down
20 changes: 20 additions & 0 deletions tests/fiber.md
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,10 @@ let string_even x =
let crash_on_three x =
if x = 3 then failwith "Simulated error"
else string_even x

let even_or_odd x =
if is_even x then Either.Right (string_of_int x)
else Either.Left (string_of_int x)
```

```ocaml
Expand Down Expand Up @@ -502,6 +506,22 @@ let crash_on_three x =
- : unit = ()
```

```ocaml
# Eio_mock.Backend.run @@ fun () ->
Fiber.List.partition_map (process even_or_odd) [1; 2; 3; 4]
|> traceln "%a" Fmt.Dump.(pair (list string) (list string));;
+Start 1
+Start 2
+Start 3
+Start 4
+Finished 1
+Finished 2
+Finished 3
+Finished 4
+(["1"; "3"], ["2"; "4"])
- : unit = ()
```

If any fiber raises, everything is cancelled:

```ocaml
Expand Down
Loading