diff --git a/core/CCOpt.ml b/core/CCOpt.ml index 40bd8e58..b99176de 100644 --- a/core/CCOpt.ml +++ b/core/CCOpt.ml @@ -84,6 +84,10 @@ let map2 f o1 o2 = match o1, o2 with | _, None -> None | Some x, Some y -> Some (f x y) +let filter p = function + | Some x as o when p x -> o + | o -> o + let iter f o = match o with | None -> () | Some x -> f x diff --git a/core/CCOpt.mli b/core/CCOpt.mli index 48b7ec4a..838f04e7 100644 --- a/core/CCOpt.mli +++ b/core/CCOpt.mli @@ -60,6 +60,11 @@ val iter : ('a -> unit) -> 'a t -> unit val fold : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a (** Fold on 0 or 1 elements *) +val filter : ('a -> bool) -> 'a t -> 'a t +(** Filter on 0 or 1 elements + + @since NEXT_RELEASE *) + val get : 'a -> 'a t -> 'a (** [get default x] unwraps [x], but if [x = None] it returns [default] instead. @since NEXT_RELEASE *)