adding opt_map to simplify result function application over optionals (#397)

adding `Result.opt_map` to simplify result function application over optionals
This commit is contained in:
Ewen Maclean 2022-01-25 20:05:11 +00:00 committed by GitHub
parent a13fc12ff4
commit 4e79b72306
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 0 deletions

View file

@ -68,6 +68,14 @@ let of_exn_trace e =
in
Error res
let opt_map f e = match e with
| None -> Ok None
| Some x ->
begin match f x with
| Ok x -> Ok (Some x)
| Error e -> Error e
end
let map f e = match e with
| Ok x -> Ok (f x)
| Error s -> Error s

View file

@ -63,6 +63,10 @@ val add_ctxf : ('a, Format.formatter, unit, ('b, string) t -> ('b, string) t) fo
]}
@since 1.2 *)
val opt_map : ('a -> ('b, 'c) t) -> 'a option -> ('b option, 'c) t
(** Map a fallible operation through an option.
@since NEXT_RELEASE *)
val map : ('a -> 'b) -> ('a, 'err) t -> ('b, 'err) t
(** Map on success. *)