Introducing Objective newLISP

Started by itistoday, December 08, 2009, 04:20:24 PM

Previous topic - Next topic

itistoday

My apologies for the delay (I said I would announce this sooner), but it is finally done!



Check it out and let me know what you think!



http://www.taoeffect.com/blog/2009/12/introducing-objective-newlisp/">Introducing Objective newLISP



Edit]
Get your Objective newLISP groove on.

Kazimir Majorinc

#1
http://www.taoeffect.com/blog/2009/12/introducing-objective-newlisp/">http://www.taoeffect.com/blog/2009/12/i ... e-newlisp/">http://www.taoeffect.com/blog/2009/12/introducing-objective-newlisp/
http://kazimirmajorinc.com/\">WWW site; http://kazimirmajorinc.blogspot.com\">blog.

m i c h a e l

#2
The link takes us to the Mutable objects in FOOP in development v.10.1.8 thread. Is this what you intended? I can't see any new links to your OOP module. Did I overlook it or are you just teasing us ;-)



m i c h a e l



P.S. Kazimir posted the link just before I posted. Thanks, Kazimir!

cormullion

#3
Cool. Well done!



To paraphrase that old joke about buses - you can wait for Object Oriented frameworks for ages and then suddenly two come along at the same time. I'm just trying to digest the new stuff Lutz posted, and now there's loads more to look at. Not complaining - that's great!



I think you could usefully say what Objective newLISP consists of - my first thought was that you had written a new language. :) It's just a module, though? So it can be mixed with existing newLISP code...



I can't say much about the introduction you've written - it's excellently done, but it's just theory to me, and I'd need to see some simple practical examples or descriptions of suitable applications before I could understand what you're aiming for with OnL, and why it would be worth getting into it. Don't worry about that too much, though, because I'm no OO programmer anyway, so these things typically operate above my head.



But - in any case - great stuff!

itistoday

#4
Quote from: "cormullion"I think you could usefully say what Objective newLISP consists of - my first thought was that you had written a new language. :) It's just a module, though? So it can be mixed with existing newLISP code...


Yes.


QuoteI can't say much about the introduction you've written - it's excellently done, but it's just theory to me, and I'd need to see some simple practical examples or descriptions of suitable applications before I could understand what you're aiming for with OnL, and why it would be worth getting into it.


You'll probably want to check out the code for this then:



http://newlispfanclub.alh.net/forum/viewtopic.php?f=17&t=3411">http://newlispfanclub.alh.net/forum/vie ... =17&t=3411">http://newlispfanclub.alh.net/forum/viewtopic.php?f=17&t=3411



(http://www.taoeffect.com/newlisp/database.lsp.html">Future proof link)


QuoteBut - in any case - great stuff!


Thanks!
Get your Objective newLISP groove on.

cormullion

#5
Quote from: "itistoday"You'll probably want to check out the code for this then:


Yes, I looked at  that... What would be helpful for me (yet to grasp all the OO benefits) would be an example set in a simpler and more familiar problem domain - complex numbers, simple geometry, etc. How about doing a quick comparison of that complex number class that michael did?


(define (Class:Class) (cons (context) (args)))
(new Class 'Complex)
(define (Complex:rad c)
    (sqrt (add (pow (c 1) ) (pow (c 2)))))
(define (Complex:add a b)
    (Complex (add (a 1) (b 1)) (add (a 2) (b 2))))
(define (Complex:mul a b)
    (let (a.re (a 1) a.im (a 2) b.re (b 1) b.im (b 2))
        (Complex
            (sub (mul a.re b.re) (mul a.im b.im))
            (add (mul a.re b.im) (mul a.im b.re)))))

m i c h a e l

#6
With the addition of the Class context as a built-in (giving us the Class:Class constructor to inherit), the new method syntax (object implicitly passed) and the self function, this would be rewritten today as:


(new Class 'Complex)
(define (Complex:rad)
    (sqrt (add (pow (self 1) ) (pow (self 2))))
)
(define (Complex:add other)
    (setf (self 1) (add (self 1) (other 1)))
    (setf (self 2) (add (self 2) (other 2)))
    (self)
)
(define (Complex:mul other) (let
    (
        a.re (self 1) a.im (self 2)
        b.re (other 1) b.im (other 2)
    )
    (setf (self 1) (sub (mul a.re b.re) (mul a.im b.im)))
    (setf (self 2) (add (mul a.re b.im) (mul a.im b.re)))
    (self)
))


m i c h a e l

cormullion

#7
(I had an email notification of your reply - thanks Ryon!)



Is that FOOP or ObjNL...?

m i c h a e l

#8
FOOP!



It's a direct copy of the code you posted above using the latest version of FOOP.



m i c h a e l

cormullion

#9
I'm thinking about trying out ObjNL for a project. But I can't get started... Fancy helping me?



The general idea is to create a book, containing chapters, each of which contain sections, each of which contain paragraphs. Would ObjNL be a good way to explore/develop this kind of structure?



I've stared at this code for a few minutes now, and I'm not getting it. What is it doing? What is a Foo and a Bar anyway...?


(context Foo)
(define (Foo:Foo _bar)
    (setf bar _bar)
    true
)

(context Bar)
(define (Bar:Bar _foo)
    (setf foo _foo)
    true ; don't allow ourselves to be deallocated
)


Anyway, a few hints would be great...!

itistoday

#10
Quote from: "cormullion"I'm thinking about trying out ObjNL for a project. But I can't get started... Fancy helping me?



The general idea is to create a book, containing chapters, each of which contain sections, each of which contain paragraphs. Would ObjNL be a good way to explore/develop this kind of structure?


It certainly can be used for that.


QuoteI've stared at this code for a few minutes now, and I'm not getting it. What is it doing? What is a Foo and a Bar anyway...?


Foo and Bar are classes. Have you done OOP before?



Remember though that you need to create them using the (new-class) function. I.e. (new-class 'Foo).



If you haven't done OOP before, I would recommend reading up on that, then the blog post on ObjNL should be perfectly clear. Let me know if there are any other questions I can answer.
Get your Objective newLISP groove on.

m i c h a e l

#11
cormullion!



foo, bar, and baz are the names often given to variables in code examples. "Foo" and "bar" were inspired by the acronym FUBAR, which stands for "F***ed up Beyond all Recognition."



The early MIT hackers popularized many terms introduced during WWII, such as FUBAR and kludge.



m i c h a e l



BTW, here is a FOOP version ;-)


(new Class 'Book)
(new Class 'Chapter)
(new Class 'Paragraph)

(define (Paragraph:text) (self 1))
(define (Chapter:text)
(format
"Chapter %dn%sn"
(self 1) (join (map (curry :text) (2 (self))) "n")
)
)
(define (Book:text)
(format
"%snn%snThe Endn"
(self 1) (join (map (curry :text) (2 (self))) "n")
)
)

(setq frankenstein
(Book "Frankenstein"
(Chapter 1
(Paragraph "I am by birth a Genevese; and my family is . . .")
(Paragraph "As the circumstances of his marriage illustrate . . .")
(Paragraph "Beaufort had taken effectual measures to conceal . . .")
)
(Chapter 2
(Paragraph "We were brought up together; there was not quite . . .")
(Paragraph "On the birth of a second son, my junior by . . .")
(Paragraph "No human being could have passed a happier . . .")
)
(Chapter 3
(Paragraph "When I had attained the age of seventeen, my . . .")
(Paragraph "Elizabeth had caught the scarlet fever; her . . .")
(Paragraph "She died calmly; and her countenance expressed . . .")
)
)
)

(println (:text frankenstein))

cormullion

#12
Thanks michael - example code is my favourite way of learning...!



I don't do much OOP - but am familiar with the concepts, in languages ranging from SmallTalk to Java. But I'd just like to see an example of ObjNL that makes sense at the same level as michael's example. I don't 'get' why you construct instances of each object to be the other. For me, the words foo and bar (I've heard of the tradition :) quickly become noise words, too devoid of meaning to be informative.

itistoday

#13
cormullion, I would check to see if FOOP satisfies your needs for your book project. ObjNL is for situations where you have complicated object relationships, and objects are being passed around and modified.



To use ObjNL properly, you also need to correctly make use of the functions retain and release, and possibly autorelease as well. These are documented a little bit in the blog post, but for a better understanding of them you may want to read the documentation on Objective-C, or some other language where you do manual reference counting.



Also, notice that the "FOOP way" uses the class name as the "constructor" of the objects (e.g.: (Paragraph "blah blah")). Normally ObjNL objects are instantiated using the instantiate function, like so:


(instantiate Paragraph "blah blah")

You don't have to do this, however, if you write your constructor like so:


(new-class 'Paragraph)
(new-class 'Chapter)

(context Paragraph)
(define (Paragraph:Paragraph _str)
(if (= @self @class)
(autorelease (instantiate @class _str))
(begin (setf str _str) true) ; we don't retain _str b/c it's not an ObjNL object
)
)

(context Chapter)
(define (Chapter:Chapter _paragraph)
(if (= @self @class)
(autorelease (instantiate @class _paragraph))
(setf paragraph (retain _paragraph)) ; we don't have a 'true' at the end so that we're deallocated if given nil
)
)


That allows you to create paragraphs like so:


(Paragraph "my great paragraph")

You get essentially identical looking code to michael's example with Chapter/Paragraph/etc. classes. However, unlike FOOP, because ObjNL allows you to pass objects around by reference, you also have to correctly manage their memory. That means using proper conventions and the memory management functions I mentioned previously.



So, unless you want to learn http://stackoverflow.com/questions/6578/understanding-reference-counting-with-cocoa-objective-c">learn about reference counting, I would stick to FOOP for this project. If FOOP's limitations start getting to you though, ObjNL should handle all of your object-oriented needs.



As an aside, I've just finished adding a basic ORM layer to DF.DB that I've tested using Sqliite3, and it uses ObjNL. It's already in the mercurial repository, and will be in the next release of Dragonfly. :-)
Get your Objective newLISP groove on.

itistoday

#14
BTW, to get exactly michael's version for Chapter, this would be the required ObjNL constructor:


(define (Chapter:Chapter _num)
(if (= @self @class)
(autorelease (eval (extend (list 'instantiate @class _num) $args)))
(set 'num _num 'paragraphs (map retain $args))
)
)


Edit: If you didn't care about calling 'instantiate' yourself, the constructor is a bit simpler:


(define (Chapter:Chapter _num)
(set 'num _num 'paragraphs (map retain $args))
)
Get your Objective newLISP groove on.