From 26e1397a4a4f4e067c990272387fee43f64e5538 Mon Sep 17 00:00:00 2001 From: carm Date: Sun, 30 Nov 2014 14:13:52 -0500 Subject: [PATCH] CCOpt: add get_lazy convenience function --- core/CCOpt.ml | 4 ++++ core/CCOpt.mli | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/core/CCOpt.ml b/core/CCOpt.ml index b99176de..6f17e981 100644 --- a/core/CCOpt.ml +++ b/core/CCOpt.ml @@ -104,6 +104,10 @@ let get_exn = function | Some x -> x | None -> invalid_arg "CCOpt.get_exn" +let get_lazy default_fn x = match x with + | None -> default_fn () + | Some y -> y + let sequence_l l = let rec aux acc l = match l with | [] -> Some (List.rev acc) diff --git a/core/CCOpt.mli b/core/CCOpt.mli index 068631fa..7bca99e5 100644 --- a/core/CCOpt.mli +++ b/core/CCOpt.mli @@ -73,6 +73,10 @@ val get_exn : 'a t -> 'a (** Open the option, possibly failing if it is [None] @raise Invalid_argument if the option is [None] *) +val get_lazy : (unit -> 'a) -> 'a t -> 'a +(** [get_lazy default_fn x] unwraps [x], but if [x = None] it returns [default_fn ()] instead. + @since NEXT_RELEASE *) + 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