mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
CCBool: Add functions if_then and if_then_else
This commit is contained in:
parent
fdb7c0f4b0
commit
77bfa34355
3 changed files with 25 additions and 3 deletions
|
|
@ -1,12 +1,22 @@
|
||||||
(* This file is free software, part of containers. See file "license" for more details. *)
|
(* This file is free software, part of containers. See file "license" for more details. *)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
type t = bool
|
type t = bool
|
||||||
|
|
||||||
let equal (a : bool) b = Stdlib.( = ) a b
|
let equal (a : bool) b = Stdlib.( = ) a b
|
||||||
let compare (a : bool) b = Stdlib.compare a b
|
let compare (a : bool) b = Stdlib.compare a b
|
||||||
|
|
||||||
|
let if_then f x =
|
||||||
|
if x then
|
||||||
|
Some (f ())
|
||||||
|
else
|
||||||
|
None
|
||||||
|
|
||||||
|
let if_then_else f g x =
|
||||||
|
if x then
|
||||||
|
f ()
|
||||||
|
else
|
||||||
|
g ()
|
||||||
|
|
||||||
let to_int (x : bool) : int =
|
let to_int (x : bool) : int =
|
||||||
if x then
|
if x then
|
||||||
1
|
1
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,14 @@ val compare : t -> t -> int
|
||||||
val equal : t -> t -> bool
|
val equal : t -> t -> bool
|
||||||
(** [equal b1 b2] is [true] if [b1] and [b2] are the same. *)
|
(** [equal b1 b2] is [true] if [b1] and [b2] are the same. *)
|
||||||
|
|
||||||
|
val if_then : (unit -> 'a) -> t -> 'a option
|
||||||
|
(** [if_then f x] is [Some (f ())] if [x] is true and None otherwise.
|
||||||
|
@since NEXT_RELEASE *)
|
||||||
|
|
||||||
|
val if_then_else : (unit -> 'a) -> (unit -> 'a) -> t -> 'a
|
||||||
|
(** [if_then_else f g x] is [f ()] if [x] is true and [g ()] otherwise.
|
||||||
|
@since NEXT_RELEASE *)
|
||||||
|
|
||||||
val to_int : t -> int
|
val to_int : t -> int
|
||||||
(** [to_int true = 1], [to_int false = 0].
|
(** [to_int true = 1], [to_int false = 0].
|
||||||
@since 2.7 *)
|
@since 2.7 *)
|
||||||
|
|
|
||||||
|
|
@ -8,4 +8,8 @@ eq true (of_int 1);;
|
||||||
eq false (of_int 0);;
|
eq false (of_int 0);;
|
||||||
eq true (of_int 42);;
|
eq true (of_int 42);;
|
||||||
eq true (of_int max_int);;
|
eq true (of_int max_int);;
|
||||||
eq true (of_int min_int)
|
eq true (of_int min_int);;
|
||||||
|
eq (Some "true") (if_then (Fun.const "true") true);;
|
||||||
|
eq None (if_then (Fun.const "true") false);;
|
||||||
|
eq "true" (if_then_else (Fun.const "true") (Fun.const "false") true);;
|
||||||
|
eq "false" (if_then_else (Fun.const "true") (Fun.const "false") false)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue