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 Lwt_result.collect without deps #1040

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
8 changes: 8 additions & 0 deletions src/core/lwt_result.ml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ let iter_error f r =
| Error e -> f e
| Ok _ -> Lwt.return_unit)

let collect x =
let rec aux oks errors = function
| [] -> if errors = [] then Ok (List.rev oks) else Error (List.rev errors)
| Ok o :: t -> aux (o :: oks) errors t
| Error e :: t -> aux oks (e :: errors) t
in
x |> Lwt.all |> Lwt.map (fun r -> aux [] [] r)

module Infix = struct
let (>>=) = bind
let (>|=) e f = map f e
Expand Down
8 changes: 8 additions & 0 deletions src/core/lwt_result.mli
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ val iter_error : ('e -> unit Lwt.t) -> ('a, 'e) t -> unit Lwt.t
@since Lwt 5.6.0
*)

val collect : ('a, 'b) result Lwt.t list -> ('a list, 'b list) result Lwt.t
(** [collect r] is a single promise resolved with [Ok u] if all promises
in [r] resolve with [Ok v] and a single promise resolved with [Error w]
otherwise.

@since NEXT_RELEASE
*)

module Infix : sig
val (>|=) : ('a,'e) t -> ('a -> 'b) -> ('b,'e) t
val (>>=) : ('a,'e) t -> ('a -> ('b,'e) t) -> ('b,'e) t
Expand Down
Loading