In Pharo, Booleans have a superb implementation!
&
, |
, not
(eager)or:
, and:
(lazy)ifTrue:ifFalse:
, ifFalse:ifTrue:
not
(Not)|
(Or)Propose an implementation of Not in a world where:
true
, false
not
? false not
-> true
true not
-> false
The solution does not use conditionals (i.e., no if
)
Boolean
(abstract), True
and False
true
is the singleton instance of True
false
is the singleton instance of False
In OOP, choice is expressed
Example
x open
x
can be a file, a window, a tool,... x
's classFalse >> not
"Negation -- answer true since the receiver is false."
^ true
True >> not
"Negation -- answer false since the receiver is true."
^ false
Boolean
is abstractTrue
and False
and implement&
, not
and:
, or:
, ifTrue:
, ifFalse:
, ifTrue:ifFalse:
, ifFalse:ifTrue:
Boolean>>not
"Abstract method. Negation: Answer true if the receiver is false, answer false if the receiver is true."
self subclassResponsibility
true | true -> true
true | false -> true
true | anything -> true
false | true -> true
false | false -> false
false | anything -> anything
Boolean >> | aBoolean
"Abstract method. Evaluating Or: Evaluate the argument.
Answer true if either the receiver or the argument is true."
self subclassResponsibility
false | true -> true
false | false -> false
false | anything -> anything
False >> | aBoolean
"Evaluating Or -- answer with the argument, aBoolean."
^ aBoolean
true | true -> true
true | false -> true
true | anything -> true
True >> | aBoolean
"Evaluating Or -- answer true since the receiver is true."
^ true
The object true
is the receiver of the message!
True>> | aBoolean
"Evaluating disjunction (Or) -- answer true since the receiver is true."
^ true
So we can write it like the following:
True >> | aBoolean
"Evaluating disjunction (Or) -- answer true since the receiver is true."
^ self
/