diff --git a/src/stdune/result.ml b/src/stdune/result.ml index 0fd5e55f..07736e7b 100644 --- a/src/stdune/result.ml +++ b/src/stdune/result.ml @@ -29,3 +29,25 @@ module O = struct let ( >>= ) t f = bind t ~f let ( >>| ) t f = map t ~f end + +open O + +let all = + let rec loop acc = function + | [] -> Ok (List.rev acc) + | t :: l -> + t >>= fun x -> + loop (x :: acc) l + in + fun l -> loop [] l + +let concat_map = + let rec loop f acc = function + | [] -> Ok (List.rev acc) + | x :: l -> + f x >>= fun y -> + loop f (List.rev_append y acc) l + in + fun l ~f -> loop f [] l + +type ('a, 'error) result = ('a, 'error) t diff --git a/src/stdune/result.mli b/src/stdune/result.mli index 7c8bb720..cda0cbe2 100644 --- a/src/stdune/result.mli +++ b/src/stdune/result.mli @@ -16,3 +16,13 @@ val map : ('a, 'error) t -> f:('a -> 'b) -> ('b, 'error) t val bind : ('a, 'error) t -> f:('a -> ('b, 'error) t) -> ('b, 'error) t val map_error : ('a, 'error1) t -> f:('error1 -> 'error2) -> ('a, 'error2) t + +val all : ('a, 'error) t list -> ('a list, 'error) t + +val concat_map + : 'a list + -> f:('a -> ('b list, 'error) t) + -> ('b list, 'error) t + +(** For compatibility with some other code *) +type ('a, 'error) result = ('a, 'error) t