newLISP Fan Club

Forum => newLISP newS => Topic started by: itistoday on December 08, 2009, 04:20:24 PM

Title: Introducing Objective newLISP
Post by: itistoday on December 08, 2009, 04:20:24 PM
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!



Introducing Objective newLISP (//http)



Edit]
Title: Re: Introducing Objective newLISP
Post by: Kazimir Majorinc on December 08, 2009, 06:16:50 PM
http://www.taoeffect.com/blog/2009/12/introducing-objective-newlisp/
Title: Re: Introducing Objective newLISP
Post by: m i c h a e l on December 08, 2009, 06:24:52 PM
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!
Title: Re: Introducing Objective newLISP
Post by: cormullion on December 09, 2009, 11:30:51 AM
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!
Title: Re: Introducing Objective newLISP
Post by: itistoday on December 09, 2009, 12:55:19 PM
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



(Future proof link (//http))


QuoteBut - in any case - great stuff!


Thanks!
Title: Re: Introducing Objective newLISP
Post by: cormullion on December 13, 2009, 04:01:54 AM
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)))))
Title: Re: Introducing Objective newLISP
Post by: m i c h a e l on December 13, 2009, 08:45:08 AM
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
Title: Re: Introducing Objective newLISP
Post by: cormullion on December 13, 2009, 09:49:03 AM
(I had an email notification of your reply - thanks Ryon!)



Is that FOOP or ObjNL...?
Title: Re: Introducing Objective newLISP
Post by: m i c h a e l on December 13, 2009, 10:21:45 AM
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
Title: Re: Introducing Objective newLISP
Post by: cormullion on February 19, 2010, 10:45:37 AM
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...!
Title: Re: Introducing Objective newLISP
Post by: itistoday on February 19, 2010, 12:06:47 PM
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.
Title: Re: Introducing Objective newLISP
Post by: m i c h a e l on February 19, 2010, 12:56:40 PM
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))
Title: Re: Introducing Objective newLISP
Post by: cormullion on February 19, 2010, 02:36:10 PM
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.
Title: Re: Introducing Objective newLISP
Post by: itistoday on February 19, 2010, 05:35:02 PM
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 learn about reference counting (//http), 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. :-)
Title: Re: Introducing Objective newLISP
Post by: itistoday on February 19, 2010, 05:56:06 PM
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))
)
Title: Re: Introducing Objective newLISP
Post by: hilti on February 19, 2010, 09:40:05 PM
Quote from: "m i c h a e l"

(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))


Hi m i c h a e l!



I'm just learning FOOP in newLISP from example code. The hardest part is to find some written in newLISP ;-)

Your "Book example" is great but I get this error:



ERR: invalid function in function format : (MAIN:self 1)
called from user defined function Book:text


May You help me?



Thanks!

Marc
Title: Re: Introducing Objective newLISP
Post by: itistoday on February 19, 2010, 09:49:35 PM
Quote from: "hilti"
ERR: invalid function in function format : (MAIN:self 1)
called from user defined function Book:text


It works fine on my end, I'm guessing you're running an older version of newLISP that doesn't support the new FOOP stuff.
Title: Re: Introducing Objective newLISP
Post by: cormullion on February 20, 2010, 11:19:21 AM
Quote from: "itistoday"ObjNL is for situations where you have complicated object relationships, and objects are being passed around and modified.


Ah, yes. I'm very grateful for the advice. I think I'm more interested in modelling the hierarchical structure rather than the interactions of objects, so you're probably right...! I shall get round to ObjNL soon.
Title: Re: Introducing Objective newLISP
Post by: m i c h a e l on February 20, 2010, 11:21:09 AM
Hi Mark!


Quote from: "hilti"I'm just learning FOOP in newLISP from example code.

The hardest part is to find some written in newLISP ;-)


:-) I'm nearing completion of a new video on FOOP, but it's just a basic overview. The next video I'd like to do should contain a more complex example, so I'm hoping that one will clear up any grey areas. Although, to be honest, there's a lot about FOOP I still don't understand myself ;-)


Quote from: "hilti"Your "Book example" is great but I get this error:



Code:

ERR: invalid function in function format : (MAIN:self 1)

called from user defined function Book:text





May You help me?


I'd be happy to, but I think Greg already nailed it. newLISP gained the self function in v.10.1.8 and had a bug fix in v.10.1.9. self is still only available through a development release.



m i c h a e l