diff --git a/core/CCOpt.ml b/core/CCOpt.ml index 07be2451..a1dd5395 100644 --- a/core/CCOpt.ml +++ b/core/CCOpt.ml @@ -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] diff --git a/core/CCOpt.mli b/core/CCOpt.mli index 2c0e1d4f..e067a097 100644 --- a/core/CCOpt.mli +++ b/core/CCOpt.mli @@ -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} *)