Introducing Objective newLISP

Notices and updates
Locked
itistoday
Posts: 429
Joined: Sun Dec 02, 2007 5:10 pm
Contact:

Introducing Objective newLISP

Post by 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!

Introducing Objective newLISP

Edit: link fixed, thanks!
Last edited by itistoday on Wed Dec 09, 2009 2:47 am, edited 1 time in total.
Get your Objective newLISP groove on.


m i c h a e l
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA
Contact:

Re: Introducing Objective newLISP

Post by m i c h a e l »

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
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: Introducing Objective newLISP

Post by cormullion »

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
Posts: 429
Joined: Sun Dec 02, 2007 5:10 pm
Contact:

Re: Introducing Objective newLISP

Post by itistoday »

cormullion wrote: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.
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.
You'll probably want to check out the code for this then:

http://newlispfanclub.alh.net/forum/vie ... =17&t=3411

(Future proof link)
But - in any case - great stuff!
Thanks!
Get your Objective newLISP groove on.

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: Introducing Objective newLISP

Post by cormullion »

itistoday wrote: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?

Code: Select all

(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
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA
Contact:

Re: Introducing Objective newLISP

Post by m i c h a e l »

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:

Code: Select all

(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
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: Introducing Objective newLISP

Post by cormullion »

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

Is that FOOP or ObjNL...?

m i c h a e l
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA
Contact:

Re: Introducing Objective newLISP

Post by m i c h a e l »

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
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: Introducing Objective newLISP

Post by 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?

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...?

Code: Select all

(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
Posts: 429
Joined: Sun Dec 02, 2007 5:10 pm
Contact:

Re: Introducing Objective newLISP

Post by itistoday »

cormullion wrote: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.
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...?
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
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA
Contact:

Re: Introducing Objective newLISP

Post by m i c h a e l »

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

Code: Select all

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

(define (Paragraph:text) (self 1))
(define (Chapter:text) 
	(format
		"Chapter %d\n%s\n" 
		(self 1) (join (map (curry :text) (2 (self))) "\n")
	)
)
(define (Book:text) 
	(format 
		"%s\n\n%s\nThe End\n" 
		(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
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: Introducing Objective newLISP

Post by cormullion »

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
Posts: 429
Joined: Sun Dec 02, 2007 5:10 pm
Contact:

Re: Introducing Objective newLISP

Post by itistoday »

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:

Code: Select all

(instantiate Paragraph "blah blah")
You don't have to do this, however, if you write your constructor like so:

Code: Select all

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

Code: Select all

(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, 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
Posts: 429
Joined: Sun Dec 02, 2007 5:10 pm
Contact:

Re: Introducing Objective newLISP

Post by itistoday »

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

Code: Select all

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

Code: Select all

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

hilti
Posts: 140
Joined: Sun Apr 19, 2009 10:09 pm
Location: Hannover, Germany
Contact:

Re: Introducing Objective newLISP

Post by hilti »

m i c h a e l wrote:

Code: Select all

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

(define (Paragraph:text) (self 1))
(define (Chapter:text) 
	(format
		"Chapter %d\n%s\n" 
		(self 1) (join (map (curry :text) (2 (self))) "\n")
	)
)
(define (Book:text) 
	(format 
		"%s\n\n%s\nThe End\n" 
		(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:

Code: Select all

ERR: invalid function in function format : (MAIN:self 1)
called from user defined function Book:text
May You help me?

Thanks!
Marc
--()o Dragonfly web framework for newLISP
http://dragonfly.apptruck.de

itistoday
Posts: 429
Joined: Sun Dec 02, 2007 5:10 pm
Contact:

Re: Introducing Objective newLISP

Post by itistoday »

hilti wrote:

Code: Select all

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.
Get your Objective newLISP groove on.

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: Introducing Objective newLISP

Post by cormullion »

itistoday wrote: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.

m i c h a e l
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA
Contact:

Re: Introducing Objective newLISP

Post by m i c h a e l »

Hi Mark!
hilti wrote: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 ;-)
hilti wrote: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

Locked