Add CCOpt.return_if

This commit is contained in:
Etienne Millon 2018-04-24 16:04:26 +02:00 committed by Simon Cruanes
parent bbda79bbcc
commit b27acb9bd2
2 changed files with 17 additions and 0 deletions

View file

@ -218,3 +218,14 @@ let flatten = function
flatten (Some None) = None
flatten (Some (Some 1)) = Some 1
*)
let return_if b x =
if b then
Some x
else
None
(*$T
return_if false 1 = None
return_if true 1 = Some 1
*)

View file

@ -126,6 +126,12 @@ val flatten : 'a t t -> 'a t
(** [flatten] transforms [Some x] into [x].
@since NEXT_RELEASE *)
val return_if : bool -> 'a -> 'a t
(** Apply [Some] or [None] depending on a boolean.
More precisely, [return_if false x] is [None],
and [return_if true x] is [Some x].
@since NEXT_RELEASE *)
(** {2 Infix Operators}
@since 0.16 *)