CCOpt.guard

This commit is contained in:
Simon Cruanes 2014-08-08 19:45:32 +02:00
parent 46ebb559fe
commit 579a135829
2 changed files with 10 additions and 0 deletions

View file

@ -110,6 +110,8 @@ let sequence_l l =
sequence_l [] = Some []
*)
let guard f x = try Some (f x) with Not_found -> None
let to_list o = match o with
| None -> []
| Some x -> [x]

View file

@ -65,6 +65,14 @@ val get_exn : 'a t -> 'a
@raise Invalid_argument if the option is [None] *)
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} *)