Search

Dark theme | Light theme

September 25, 2012

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, then B is true. So if A is true then it is implied B is true as well. If A is false then 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)