adding opt_map to simplify result function application over optionals

This commit is contained in:
Ewen Maclean 2022-01-25 17:09:17 +00:00
parent a13fc12ff4
commit 289fc8af7b
2 changed files with 9 additions and 0 deletions

View file

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

View file

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