Skip to content

Commit

Permalink
adding opt_map to simplify result function application over optionals (
Browse files Browse the repository at this point in the history
…#397)

adding `Result.opt_map` to simplify result function application over optionals
  • Loading branch information
ewenmaclean authored Jan 25, 2022
1 parent a13fc12 commit 4e79b72
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/core/CCResult.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/core/CCResult.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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. *)

Expand Down

0 comments on commit 4e79b72

Please sign in to comment.