Foop, the movie

Started by cormullion, December 08, 2007, 07:33:28 AM

Previous topic - Next topic

cormullion

Thanks for the movie!



http://nuevatec.com/FOOP-00-01.mov">//http://nuevatec.com/FOOP-00-01.mov

newBert

#1
Fantastic movie ! I can't wait for Chapter 2 !



It made me want to put into practice at once with this little script (inspired by a Python script):


#!/usr/bin/newlisp
; newlisp 2.9.7 - dec 2007
; This script creates objects 'Atom' and 'Ion'.
; A ion is just an altered atom. So the class 'Ion'
; is derived from the class 'Atom'and inherits from this one
; all its attributes and methods, appending its own.
; One of these added methods (the method 'display') replaces
; a method of the same name inherited from 'Atom'.
; Then 'Atom' and 'Ion' have both a method with the same name
; but making different work. That is polymorphism.

(set 'table '(nil
("hydrogen" 0)("helium" 2)("lithium" 4)
("beryllium" 5)("boron" 6)("carbon" 6)
("nitrogen" 7)("oxygen" 8)("fluorine" 10)("neon" 10)))

(define (Atome:Atome nat , np ne nn)
; nat=atomic number
(set 'np nat 'ne nat 'nn (table nat 1))
(list Atome nat np ne nn))

(define (Atome:display a)
(println "nName of the element : " (table (a 2) 0))
(println (a 2) " protons, " (a 3) " electrons, " (a 4) " neutrons"))

(new Atome 'Ion)

(define (Ion:Ion nat charge , np ne nn)
(set 'np nat 'ne (- nat charge) 'nn (table nat 1))
(list Ion nat np ne nn charge))

(define (Ion:display a)
(Atome:display a)
(println "Electrified particle. Charge = " (a 5)))

;; Main Program

(set 'a1 (Atome 5))
(set 'a2 (Ion 3 1))
(set 'a3 (Ion 8 -2))

(:display a1)
(:display a2)
(:display a3)


I'm not sure it is perfect, concerning FOOP (and my knowledges about physics)

;)
<r><I>>Bertrand<e></e></I> − <COLOR color=\"#808080\">><B>newLISP<e></e></B> v.10.7.6 64-bit <B>>on Linux<e></e></B> (<I>>Linux Mint 20.1<e></e></I>)<e></e></COLOR></r>

cormullion

OT!
#2
And when you mention elements in the periodic table just after I've watched michael's wonderful animated movie, I can't resist pointing you to this wonderful flash animation:



http://www.privatehand.com/flash/elements.html">//http://www.privatehand.com/flash/elements.html

newBert

#3
Quote from: "cormullion"And when you mention elements in the periodic table just after I've watched michael's wonderful animated movie, I can't resist pointing you to this wonderful flash animation:



http://www.privatehand.com/flash/elements.html">//http://www.privatehand.com/flash/elements.html

Fine and funny :D

Thanks
<r><I>>Bertrand<e></e></I> − <COLOR color=\"#808080\">><B>newLISP<e></e></B> v.10.7.6 64-bit <B>>on Linux<e></e></B> (<I>>Linux Mint 20.1<e></e></I>)<e></e></COLOR></r>

m i c h a e l

#4
Hi newBert!



I'm glad to see the video inspired you to experiment with FOOP. Since this way of programming with newLISP is still new, there are no FOOP experts. I call what I'm doing explorations because I'm just feeling my way around in the dark. The FOOP way emerges through use.



Your code is quite good, and the comments at the top show you clearly understand the subject. Object access occurs only within methods (display), and your use of Atome:display within Ion:display is really nice. (Here, the complex subject of calling a superclass's overridden method is turned into a simple qualified method call.)



I do have a question about your constructors. Since the values of the symbols np, ne, nn, etc. are lost with the completion of the constructor, why not this?


(define (Atome:Atome nat)
   ; nat=atomic number
   (list Atome nat nat nat (table nat 1)))




Or if you want to document the attributes:


(define (Ion:Ion nat charge)
   ;; (Ion nat np ne nn charge)
   (list Ion nat nat (- nat charge) (table nat 1) charge))




There is no single, fixed way to do FOOP, so go with the way that feels most natural to you.



Certain aspects of FOOP will require a mental shift for practiced OOPers. I believe the concept of changeless objects is the part of FOOP that will give them the most difficulty. When we begin to use objects the same way we use the number 5 or the string "hello", I think we'll be on the right track.



m i c h a e l

cormullion

#5
Quote from: "m i c h a e l" I believe the concept of changeless objects is the part of FOOP that will give them the most difficulty.


Yes, it's confusing this non-OOPer too. If I understand correctly: you're not able to modify the 'internal variables' of an object instance using these method calls. So when you want to, say, update the x and y coordinates of an object, you have to create a new copy of it with the modified values. Why can't you change the values in place, then...?



It doesn't seem right that you have to create new copies of things all the time just to change one value... One of my original comments about using contexts was that you're duplicating the methods inside each object. Now you're continually making copies of the data...

Lutz

#6
QuoteNow you're continually making copies of the data...


well, not continually, just a bit more, but in the overall picture the difference is probably less than 5%, because so much copying is going on in the interpreter internally anyway.



But you gain advantages: code writing which is said less error-prone in a functional style programming and a functional style OO melts better into a functional language.



Lutz

newBert

#7
Quote from: "m i c h a e l"


I do have a question about your constructors. Since the values of the symbols np, ne, nn, etc. are lost with the completion of the constructor, why not this?


(define (Atome:Atome nat)
   ; nat=atomic number
   (list Atome nat nat nat (table nat 1)))




Or if you want to document the attributes:


(define (Ion:Ion nat charge)
   ;; (Ion nat np ne nn charge)
   (list Ion nat nat (- nat charge) (table nat 1) charge))




There is no single, fixed way to do FOOP, so go with the way that feels most natural to you.




I've done that quickly from a Python script and I didn't take time to think about the name of the attributes that I copied out again from the original script. But I do note your remarks...

:)
<r><I>>Bertrand<e></e></I> − <COLOR color=\"#808080\">><B>newLISP<e></e></B> v.10.7.6 64-bit <B>>on Linux<e></e></B> (<I>>Linux Mint 20.1<e></e></I>)<e></e></COLOR></r>