#inject:into: What a Gem!

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

#inject:into: What a Gem!

Eric Taylor
Hello Forum,

I just accomplished something in about ten minutes that has taken me
many times that in other languages, even Eiffel.  I decided to create a
method in my ActiveTreeModel class that would build a TreeModel from the
bracketed notation of a tree (as explained in Knuth's AoP, Vol. 1).  The
bracketed notation is a very efficient way to represent a tree (in most,
but not all, circumstances).

Given the following array,

#(#a #b #(#c #d #e) #f #g #(#h #i #(#j)))

build a tree.  It should look like this:

a
b
        c
        d
        e
f
g
        h
        i
                j

I was able to accomplish this with merely the following code:

ActiveTreeModel>>addFromBracketNotation: anArray asChildOf: parent

anArray inject: nil
into:
        [:elementBefore :element |
        (element isKindOf: Array)
                ifTrue:
                        [self addFromArray: element asChildOf:
elementBefore]
                ifFalse: [self add: element asChildOf: parent]]

This kind of parsing is usually a mess in other languages.  Smalltalk
just seems to smile and take it all in stride.  I expected to have to
spend hours doing this.  I think the only reason it took a whopping ten
minutes, instead of five, was due to the need to change from "(element
countElements > 1)" to "(element isKindOf: Array)".  I was bitten by the
following scenario:

#(#b) #countElements returns 1.
#c #countElements also returns 1.

Notationally, however, the two are very different.

Cheers,

Eric


Reply | Threaded
Open this post in threaded view
|

Re: #inject:into: What a Gem!

Eric Taylor
Oops :|

A slight error: The method is recursive, so it should actually be

ActiveTreeModel>>addFromBracketNotation: anArray asChildOf: parent
 
anArray inject: nil
into:
  [:elementBefore :element |
  (element isKindOf: Array)
  ifTrue:
  [self addFromBracketNotation: element asChildOf:
 elementBefore]
  ifFalse: [self add: element asChildOf: parent]]

I changed the name of the method after I copied it into the post, but
before I had sent the post, and then edited the text in the post itself.
I overlooked the recursive call.  You know, one would think that Outlook
would have caught the syntax error :).


Cheers,

Eric

> -----Original Message-----
> From: Eric Taylor [mailto:[hidden email]]
> Posted At: Monday, July 03, 2006 9:35 PM
> Posted To: comp.lang.smalltalk.dolphin
> Conversation: #inject:into: What a Gem!
> Subject: #inject:into: What a Gem!
>
> Hello Forum,
>
> I just accomplished something in about ten minutes that has taken me
> many times that in other languages, even Eiffel.  I decided to create
a
> method in my ActiveTreeModel class that would build a TreeModel from
the
> bracketed notation of a tree (as explained in Knuth's AoP, Vol. 1).
The
> bracketed notation is a very efficient way to represent a tree (in
most,

> but not all, circumstances).
>
> Given the following array,
>
> #(#a #b #(#c #d #e) #f #g #(#h #i #(#j)))
>
> build a tree.  It should look like this:
>
> a
> b
> c
> d
> e
> f
> g
> h
> i
> j
>
> I was able to accomplish this with merely the following code:
>
> ActiveTreeModel>>addFromBracketNotation: anArray asChildOf: parent
>
> anArray inject: nil
> into:
> [:elementBefore :element |
> (element isKindOf: Array)
> ifTrue:
> [self addFromArray: element asChildOf:
> elementBefore]
> ifFalse: [self add: element asChildOf: parent]]
>
> This kind of parsing is usually a mess in other languages.  Smalltalk
> just seems to smile and take it all in stride.  I expected to have to
> spend hours doing this.  I think the only reason it took a whopping
ten
> minutes, instead of five, was due to the need to change from "(element
> countElements > 1)" to "(element isKindOf: Array)".  I was bitten by
the

> following scenario:
>
> #(#b) #countElements returns 1.
> #c #countElements also returns 1.
>
> Notationally, however, the two are very different.
>
> Cheers,
>
> Eric


Reply | Threaded
Open this post in threaded view
|

Re: #inject:into: What a Gem!

Fernando Rodríguez-2
In reply to this post by Eric Taylor
Hello Eric,

> Hello Forum,
>
> I just accomplished something in about ten minutes that has taken me
> many times that in other languages, even Eiffel.  I decided to create
> a method in my ActiveTreeModel class that would build a TreeModel from
> the bracketed notation of a tree (as explained in Knuth's AoP, Vol.
> 1).  The bracketed notation is a very efficient way to represent a
> tree (in most, but not all, circumstances).
>
> Given the following array,
>
> #(#a #b #(#c #d #e) #f #g #(#h #i #(#j)))
>


If you liked the Smalltalk solution, you should take a look at Lisp. Your
"bracketed notation" is already lisp code representing that tree, just change
# with ' and you're done. No extra parsing necessary.

Of course, you can also do it in java o C++ for extra fun and a extra klines
of code. ;-)


Reply | Threaded
Open this post in threaded view
|

Re: #inject:into: What a Gem!

Eric Taylor
Hello Fernando,

I took a look at Lisp years ago.  In engineering school, I used a
Mathematica for all of my lab write-ups (they encouraged us to use
Maple, but permitted us to use any CAS).  One of its seven different
programming paradigms is list processing.  The bracketed notation of
trees is trivial for Mathematica, as no doubt it is for Lisp (as you
indicate).

But it would seem that it is equally trivial for Smalltalk.  I am hooked
now on Smalltalk...and Dolphin :).


Cheers,

Eric

> -----Original Message-----
> From: Fernando Rodríguez [mailto:[hidden email]]
> Posted At: Wednesday, July 05, 2006 11:58 AM
> Posted To: comp.lang.smalltalk.dolphin
> Conversation: #inject:into: What a Gem!
> Subject: Re: #inject:into: What a Gem!
>
> Hello Eric,
>
> > Hello Forum,
> >
> > I just accomplished something in about ten minutes that has taken me
> > many times that in other languages, even Eiffel.  I decided to
create
> > a method in my ActiveTreeModel class that would build a TreeModel
from

> > the bracketed notation of a tree (as explained in Knuth's AoP, Vol.
> > 1).  The bracketed notation is a very efficient way to represent a
> > tree (in most, but not all, circumstances).
> >
> > Given the following array,
> >
> > #(#a #b #(#c #d #e) #f #g #(#h #i #(#j)))
> >
>
>
> If you liked the Smalltalk solution, you should take a look at Lisp.
Your
> "bracketed notation" is already lisp code representing that tree, just
> change
> # with ' and you're done. No extra parsing necessary.
>
> Of course, you can also do it in java o C++ for extra fun and a extra
> klines
> of code. ;-)