mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 19:25:28 -05:00
CCOpt.guard
This commit is contained in:
parent
46ebb559fe
commit
579a135829
2 changed files with 10 additions and 0 deletions
|
|
@ -110,6 +110,8 @@ let sequence_l l =
|
||||||
sequence_l [] = Some []
|
sequence_l [] = Some []
|
||||||
*)
|
*)
|
||||||
|
|
||||||
|
let guard f x = try Some (f x) with Not_found -> None
|
||||||
|
|
||||||
let to_list o = match o with
|
let to_list o = match o with
|
||||||
| None -> []
|
| None -> []
|
||||||
| Some x -> [x]
|
| Some x -> [x]
|
||||||
|
|
|
||||||
|
|
@ -65,6 +65,14 @@ val get_exn : 'a t -> 'a
|
||||||
@raise Invalid_argument if the option is [None] *)
|
@raise Invalid_argument if the option is [None] *)
|
||||||
|
|
||||||
val sequence_l : 'a t list -> 'a list t
|
val sequence_l : 'a t list -> 'a list t
|
||||||
|
(** [sequence_l [x1; x2; ...; xn]] returns [Some [y1;y2;...;yn]] if
|
||||||
|
every [xi] is [Some yi]. Otherwise, if the list contains at least
|
||||||
|
one [None], the result is [None]. *)
|
||||||
|
|
||||||
|
val guard : ('a -> 'b) -> 'a -> 'b option
|
||||||
|
(** [guard f x] calls [f x] and returns [Some y] if [f x = y]. If [f x] raises
|
||||||
|
any exception, the result is [None]. This can be useful to wrap functions
|
||||||
|
such as [Map.S.find]. *)
|
||||||
|
|
||||||
(** {2 Applicative} *)
|
(** {2 Applicative} *)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue