mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
Added apply_or
Added apply_or for chaining processing functions.
This commit is contained in:
parent
3c70b921cb
commit
752dd189f6
4 changed files with 33 additions and 0 deletions
|
|
@ -116,6 +116,13 @@ let get_or ~default x =
|
|||
| None -> default
|
||||
| Some y -> y
|
||||
|
||||
let apply_or f x =
|
||||
match f x with
|
||||
| None -> x
|
||||
| Some y -> y
|
||||
|
||||
let ( |?> ) x f = apply_or f x
|
||||
|
||||
let value x ~default =
|
||||
match x with
|
||||
| None -> default
|
||||
|
|
@ -186,6 +193,7 @@ module Infix = struct
|
|||
let ( <*> ) = ( <*> )
|
||||
let ( <$> ) = map
|
||||
let ( <+> ) = ( <+> )
|
||||
let ( |?> ) = ( |?> )
|
||||
let ( let+ ) = ( >|= )
|
||||
let ( let* ) = ( >>= )
|
||||
|
||||
|
|
|
|||
|
|
@ -95,6 +95,12 @@ val get_or : default:'a -> 'a t -> 'a
|
|||
returns [default] if [o] is [None].
|
||||
@since 0.18 *)
|
||||
|
||||
val apply_or : ('a -> 'a t) -> 'a -> 'a
|
||||
(** [apply_or f x] returns the original [x] if [f] fails, or unwraps [f x] if it succeeds.
|
||||
Useful for piping preprocessing functions together (such as string processing),
|
||||
turning functions like "remove" into "remove_if_it_exists".
|
||||
*)
|
||||
|
||||
val value : 'a t -> default:'a -> 'a
|
||||
(** [value o ~default] is similar to the Stdlib's [Option.value] and to {!get_or}.
|
||||
@since 2.8 *)
|
||||
|
|
@ -178,6 +184,9 @@ module Infix : sig
|
|||
val ( <+> ) : 'a t -> 'a t -> 'a t
|
||||
(** [o1 <+> o2] is [o1] if [o1] is [Some _], [o2] if [o1] is [None]. *)
|
||||
|
||||
val ( |?> ) : 'a -> ('a -> 'a t) -> 'a
|
||||
(** [x |?> f] is [apply_or f x] *)
|
||||
|
||||
val ( let+ ) : 'a t -> ('a -> 'b) -> 'b t
|
||||
val ( and+ ) : 'a t -> 'b t -> ('a * 'b) t
|
||||
val ( let* ) : 'a t -> ('a -> 'b t) -> 'b t
|
||||
|
|
|
|||
|
|
@ -101,6 +101,13 @@ let get_or e ~default =
|
|||
| Ok x -> x
|
||||
| Error _ -> default
|
||||
|
||||
let apply_or f x =
|
||||
match f x with
|
||||
| Error _ -> x
|
||||
| Ok y -> y
|
||||
|
||||
let ( |?> ) x f = apply_or f x
|
||||
|
||||
let get_lazy f e =
|
||||
match e with
|
||||
| Ok x -> x
|
||||
|
|
@ -271,6 +278,7 @@ module Infix = struct
|
|||
let ( >|= ) e f = map f e
|
||||
let ( >>= ) e f = flat_map f e
|
||||
let ( <*> ) = ( <*> )
|
||||
let ( |?> ) = ( |?> )
|
||||
let ( let+ ) = ( >|= )
|
||||
let ( let* ) = ( >>= )
|
||||
|
||||
|
|
|
|||
|
|
@ -96,6 +96,12 @@ val get_exn : ('a, _) t -> 'a
|
|||
val get_or : ('a, _) t -> default:'a -> 'a
|
||||
(** [get_or e ~default] returns [x] if [e = Ok x], [default] otherwise. *)
|
||||
|
||||
val apply_or : ('a -> ('a, _) t) -> 'a -> 'a
|
||||
(** [apply_or f x] returns the original [x] if [f] fails, or unwraps [f x] if it succeeds.
|
||||
Useful for piping preprocessing functions together (such as string processing),
|
||||
turning functions like "remove" into "remove_if_it_exists".
|
||||
*)
|
||||
|
||||
val get_or_failwith : ('a, string) t -> 'a
|
||||
(** [get_or_failwith e] returns [x] if [e = Ok x], fails otherwise.
|
||||
@raise Failure with [msg] if [e = Error msg].
|
||||
|
|
@ -192,6 +198,8 @@ module Infix : sig
|
|||
[Ok (a b)]. Otherwise, it fails, and the error of [a] is chosen
|
||||
over the error of [b] if both fail. *)
|
||||
|
||||
val ( |?> ) : 'a -> ('a -> ('a, _) t) -> 'a
|
||||
|
||||
val ( let+ ) : ('a, 'e) t -> ('a -> 'b) -> ('b, 'e) t
|
||||
(** @since 2.8 *)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue