Groovy Goodness: Boolean Implications
Since Groovy 1.8.3 we can use the implies()
method on Boolean
types. The implies()
method implements a logical implication. This means that if we have two Boolean
variables A and B, that if A is true
, than B is true
. So if A is true
than it is implied B is true
as well. If A is false
than B can be either true
or false
. We could rewrite the implication as !A or B
.
def a = true
def b = true
assert a.implies(b)
assert !(a.implies(false))
assert a.implies(b) == ((!a).or(b))
assert true.implies(true)
assert false.implies(true)
assert false.implies(false)
assert !true.implies(false)
Code written with Groovy 2.0.4