core/Bool
Boolean type and operations.
Import from the core library to use this module.
import Bool "mo:core/Bool";
While boolean operators _ and _
and _ or _
are short-circuiting,
avoiding computation of the right argument when possible, the functions
logand(_, _)
and logor(_, _)
are strict and will always evaluate both
of their arguments.
Example:
let t = true;
let f = false;
// Short-circuiting AND
assert not (t and f);
// Short-circuiting OR
assert t or f;
Type Bool
type Bool = Prim.Types.Bool
Booleans with constants true
and false
.
Function logicalAnd
func logicalAnd(a : Bool, b : Bool) : Bool
Returns a and b
.
Example:
assert not Bool.logicalAnd(true, false);
assert Bool.logicalAnd(true, true);
Function logicalOr
func logicalOr(a : Bool, b : Bool) : Bool
Returns a or b
.
Example:
assert Bool.logicalOr(true, false);
assert Bool.logicalOr(false, true);
Function logicalXor
func logicalXor(a : Bool, b : Bool) : Bool
Returns exclusive or of a
and b
, a != b
.
Example:
assert Bool.logicalXor(true, false);
assert not Bool.logicalXor(true, true);
assert not Bool.logicalXor(false, false);
Function logicalNot
func logicalNot(bool : Bool) : Bool
Returns not bool
.
Example:
assert Bool.logicalNot(false);
assert not Bool.logicalNot(true);
Function equal
func equal(a : Bool, b : Bool) : Bool
Returns a == b
.
Example:
assert Bool.equal(true, true);
assert not Bool.equal(true, false);
Function compare
func compare(a : Bool, b : Bool) : Order.Order
Returns the ordering of a
compared to b
.
Returns #less
if a
is false
and b
is true
,
#equal
if a
equals b
,
and #greater
if a
is true
and b
is false
.
Example:
assert Bool.compare(true, false) == #greater;
assert Bool.compare(true, true) == #equal;
assert Bool.compare(false, true) == #less;
Function toText
func toText(bool : Bool) : Text
Returns a text value which is either "true"
or "false"
depending on the input value.
Example:
assert Bool.toText(true) == "true";
assert Bool.toText(false) == "false";
Function allValues
func allValues() : Iter.Iter<Bool>
Returns an iterator over all possible boolean values (true
and false
).
Example:
let iter = Bool.allValues();
assert iter.next() == ?true;
assert iter.next() == ?false;
assert iter.next() == null;