Re: Alan Kay on OOP

Liste des GroupesRevenir à c misc 
Sujet : Re: Alan Kay on OOP
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.misc
Date : 27. Feb 2025, 15:11:24
Autres entêtes
Organisation : Stefan Ram
Message-ID : <Boolean-20250227150601@ram.dialup.fu-berlin.de>
References : 1 2
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 <T> T ifTrue(Supplier<T> trueBlock);
    abstract <T> T ifFalse(Supplier<T> falseBlock);
    abstract <T> T ifTrueIfFalse(Supplier<T> trueBlock, Supplier<T> falseBlock);
}

class True extends Boolean {
    @Override
    <T> T ifTrue(Supplier<T> trueBlock) {
        return trueBlock.get();
    }

    @Override
    <T> T ifFalse(Supplier<T> falseBlock) {
        return null;
    }

    @Override
    <T> T ifTrueIfFalse(Supplier<T> trueBlock, Supplier<T> falseBlock) {
        return trueBlock.get();
    }
}

class False extends Boolean {
    @Override
    <T> T ifTrue(Supplier<T> trueBlock) {
        return null;
    }

    @Override
    <T> T ifFalse(Supplier<T> falseBlock) {
        return falseBlock.get();
    }

    @Override
    <T> T ifTrueIfFalse(Supplier<T> trueBlock, Supplier<T> falseBlock) {
        return falseBlock.get();
    }
}

public class SmalltalkToJava {
    private static final Boolean TRUE = new True();
    private static final Boolean FALSE = new False();

    public static void main(String[] args) {
        int[] a = {-5};
        (a[0] < 0 ? TRUE : FALSE).ifTrue(() -> {
            a[0] = 0;
            return null;
        });
        System.out.println(a[0]);

        int[] b = {10};
        (b[0] < 0 ? TRUE : FALSE).ifTrue(() -> {
            b[0] = 0;
            return null;
        });
        System.out.println(b[0]);

        (a[0] == 0 && b[0] == 10 ? TRUE : FALSE).ifTrueIfFalse(
            () -> {
                System.out.println("Both conditions are true");
                return null;
            },
            () -> {
                System.out.println("At least one condition is false");
                return null;
            }
        );
    }
}

  , C++,

#include <iostream>
#include <functional>

class Boolean {
public:
    virtual void ifTrue(const std::function<void()>& block) = 0;
    virtual void ifFalse(const std::function<void()>& block) = 0;
    virtual void ifTrue(const std::function<void()>& trueBlock, const std::function<void()>& falseBlock) = 0;
};

class True : public Boolean {
public:
    void ifTrue(const std::function<void()>& block) override {
        block();
    }

    void ifFalse(const std::function<void()>& block) override {}

    void ifTrue(const std::function<void()>& trueBlock, const std::function<void()>& falseBlock) override {
        trueBlock();
    }
};

class False : public Boolean {
public:
    void ifTrue(const std::function<void()>& block) override {}

    void ifFalse(const std::function<void()>& block) override {
        block();
    }

    void ifTrue(const std::function<void()>& trueBlock, const std::function<void()>& falseBlock) override {
        falseBlock();
    }
};

int main() {
    True true_obj;
    False false_obj;

    int a = -5;
    (a < 0 ? true_obj : false_obj).ifTrue([&]() { a = 0; });
    std::cout << a << std::endl;

    int b = 10;
    (b < 0 ? true_obj : false_obj).ifTrue([&]() { b = 0; });
    std::cout << b << std::endl;

    (a == 0 && b == 10 ? true_obj : false_obj).ifTrue(
        []() { std::cout << "Both conditions are true" << std::endl; },
        []() { std::cout << "At least one condition is false" << std::endl; }
    );

    return 0;
}

  , C,

#include <stdio.h>
#include <stdbool.h>

typedef struct Boolean Boolean;
typedef struct True True;
typedef struct False False;

struct Boolean {
    void (*ifTrue)(Boolean* self, void (*block)(void));
    void (*ifFalse)(Boolean* self, void (*block)(void));
    void (*ifTrueIfFalse)(Boolean* self, void (*trueBlock)(void), void (*falseBlock)(void));
};

struct True {
    Boolean base;
};

struct False {
    Boolean base;
};

void True_ifTrue(Boolean* self, void (*block)(void)) {
    block();
}

void True_ifFalse(Boolean* self, void (*block)(void)) {
    // Do nothing
}

void True_ifTrueIfFalse(Boolean* self, void (*trueBlock)(void), void (*falseBlock)(void)) {
    trueBlock();
}

void False_ifTrue(Boolean* self, void (*block)(void)) {
    // Do nothing
}

void False_ifFalse(Boolean* self, void (*block)(void)) {
    block();
}

void False_ifTrueIfFalse(Boolean* self, void (*trueBlock)(void), void (*falseBlock)(void)) {
    falseBlock();
}

True true_obj = {{True_ifTrue, True_ifFalse, True_ifTrueIfFalse}};
False false_obj = {{False_ifTrue, False_ifFalse, False_ifTrueIfFalse}};

#define TRUE ((Boolean*)&true_obj)
#define FALSE ((Boolean*)&false_obj)

int main() {
    int a = -5;
    (a < 0 ? TRUE : FALSE)->ifTrue(^{
        a = 0;
    });
    printf("%d\n", a);

    int b = 10;
    (b < 0 ? TRUE : FALSE)->ifTrue(^{
        b = 0;
    });
    printf("%d\n", b);

    (a == 0 && b == 10 ? TRUE : FALSE)->ifTrueIfFalse(
        ^{ printf("Both conditions are true\n"); },
        ^{ printf("At least one condition is false\n"); }
    );

    return 0;
}

  .



Date Sujet#  Auteur
27 Feb 25 * Alan Kay on OOP7Salvador Mirzo
27 Feb 25 +* Re: Alan Kay on OOP5Stefan Ram
27 Feb 25 i+- Re: Alan Kay on OOP1Stefan Ram
27 Feb 25 i+- Re: Alan Kay on OOP1Lawrence D'Oliveiro
18 Mar 25 i`* Re: Alan Kay on OOP2Salvador Mirzo
18 Mar 25 i `- Re: Alan Kay on OOP1Stefan Ram
28 Feb 25 `- Re: Alan Kay on OOP1Lawrence D'Oliveiro

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal