CCOpt: add get_lazy convenience function

This commit is contained in:
carm 2014-11-30 14:13:52 -05:00
parent e6aabf0d77
commit 26e1397a4a
2 changed files with 8 additions and 0 deletions

View file

@ -104,6 +104,10 @@ let get_exn = function
| Some x -> x | Some x -> x
| None -> invalid_arg "CCOpt.get_exn" | 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 sequence_l l =
let rec aux acc l = match l with let rec aux acc l = match l with
| [] -> Some (List.rev acc) | [] -> Some (List.rev acc)

View file

@ -73,6 +73,10 @@ val get_exn : 'a t -> 'a
(** Open the option, possibly failing if it is [None] (** Open the option, possibly failing if it is [None]
@raise Invalid_argument if the option 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 val sequence_l : 'a t list -> 'a list t
(** [sequence_l [x1; x2; ...; xn]] returns [Some [y1;y2;...;yn]] if (** [sequence_l [x1; x2; ...; xn]] returns [Some [y1;y2;...;yn]] if
every [xi] is [Some yi]. Otherwise, if the list contains at least every [xi] is [Some yi]. Otherwise, if the list contains at least