Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.misc    |    General topics about computers not cover    |    21,759 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 20,780 of 21,759    |
|    Stefan Ram to Stefan Ram    |
|    Re: Alan Kay on OOP (1/2)    |
|    27 Feb 25 14:11:24    |
   
   From: ram@zedat.fu-berlin.de   
      
   ram@zedat.fu-berlin.de (Stefan Ram) wrote or quoted:   
   >ifTrue: aBlock   
   > ^aBlock value   
   >   
   > and "False" with   
   >   
   >ifTrue: aBlock   
   > ^nil   
      
    Heads up: All code of this post was generated and not tested!   
      
    Here's an extended program, supossed to run unter GNU Smalltalk:   
      
   Object subclass: Boolean [   
    Boolean class >> new [   
    self error: 'Boolean instances cannot be created'   
    ]   
      
    ifTrue: trueBlock [   
    self subclassResponsibility   
    ]   
      
    ifFalse: falseBlock [   
    self subclassResponsibility   
    ]   
      
    ifTrue: trueBlock ifFalse: falseBlock [   
    self subclassResponsibility   
    ]   
   ]   
      
   Boolean subclass: True [   
    ifTrue: trueBlock [   
    ^trueBlock value   
    ]   
      
    ifFalse: falseBlock [   
    ^nil   
    ]   
      
    ifTrue: trueBlock ifFalse: falseBlock [   
    ^trueBlock value   
    ]   
   ]   
      
   Boolean subclass: False [   
    ifTrue: trueBlock [   
    ^nil   
    ]   
      
    ifFalse: falseBlock [   
    ^falseBlock value   
    ]   
      
    ifTrue: trueBlock ifFalse: falseBlock [   
    ^falseBlock value   
    ]   
   ]   
      
   "Create global instances"   
   true := True new.   
   false := False new.   
      
   "Example usage"   
   a := -5.   
   a < 0 ifTrue: [a := 0].   
   a printNl.   
      
   b := 10.   
   b < 0 ifTrue: [b := 0].   
   b printNl.   
      
   (a = 0 and: [b = 10]) ifTrue: [   
    'Both conditions are true' printNl   
   ] ifFalse: [   
    'At least one condition is false' printNl   
   ].   
      
    , expected output:   
      
   0   
   10   
   Both conditions are true   
      
    , Common Lisp,   
      
   ; Define the Boolean class (in Common Lisp, we'll use structures)   
   (defstruct (boolean (:constructor nil)))   
      
   ; Define True and False subclasses   
   (defstruct (true (:include boolean)))   
   (defstruct (false (:include boolean)))   
      
   ; Create global instances   
   (defparameter *true* (make-true))   
   (defparameter *false* (make-false))   
      
   ; Define methods for True   
   (defmethod if-true ((condition true) true-block)   
    (funcall true-block))   
      
   (defmethod if-false ((condition true) false-block)   
    nil)   
      
   (defmethod if-true-false ((condition true) true-block false-block)   
    (funcall true-block))   
      
   ; Define methods for False   
   (defmethod if-true ((condition false) true-block)   
    nil)   
      
   (defmethod if-false ((condition false) false-block)   
    (funcall false-block))   
      
   (defmethod if-true-false ((condition false) true-block false-block)   
    (funcall false-block))   
      
   ; Helper function to convert boolean to our custom boolean objects   
   (defun to-boolean (value)   
    (if value *true* *false*))   
      
   ; Example usage   
   (let ((a -5))   
    (if-true (to-boolean (< a 0))   
    (lambda () (setf a 0)))   
    (print a))   
      
   (let ((b 10))   
    (if-true (to-boolean (< b 0))   
    (lambda () (setf b 0)))   
    (print b))   
      
   (if-true-false (to-boolean (and (= a 0) (= b 10)))   
    (lambda () (print "Both conditions are true"))   
    (lambda () (print "At least one condition is false")))   
      
    , Python,   
      
   from abc import ABC, abstractmethod   
      
   class Boolean(ABC):   
    @abstractmethod   
    def if_true(self, block):   
    pass   
      
    @abstractmethod   
    def if_false(self, block):   
    pass   
      
    @abstractmethod   
    def if_true_if_false(self, true_block, false_block):   
    pass   
      
   class True(Boolean):   
    def if_true(self, block):   
    return block()   
      
    def if_false(self, block):   
    return None   
      
    def if_true_if_false(self, true_block, false_block):   
    return true_block()   
      
   class False(Boolean):   
    def if_true(self, block):   
    return None   
      
    def if_false(self, block):   
    return block()   
      
    def if_true_if_false(self, true_block, false_block):   
    return false_block()   
      
   # Create global instances   
   true = True()   
   false = False()   
      
   # Example usage   
   a = -5   
   (true if a < 0 else false).if_true(lambda: globals().update(a=0))   
   print(a)   
      
   b = 10   
   (true if b < 0 else false).if_true(lambda: globals().update(b=0))   
   print(b)   
      
   (true if a == 0 and b == 10 else false).if_true_if_false(   
    lambda: print("Both conditions are true"),   
    lambda: print("At least one condition is false")   
   )   
      
    , Java,   
      
   import java.util.function.Supplier;   
      
   abstract class Boolean {   
    abstract
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca