Hi,
If write: |child parent| child := 1. parent := 2. self doThis: #(child parent) into a method the elements in the literal array are turned into symbols rather than beeing evaluated at run time. How would I have to rewrite this? Thanks Günther |
> self doThis: #(child parent)
self doThis: (Array with: child with: parent). Or if you do not need the variables: self doThis: #(1 2). CU, Udo Guenther Schmidt wrote: > Hi, > > If write: > > |child parent| > > child := 1. > parent := 2. > > self doThis: #(child parent) > > > into a method the elements in the literal array are turned into symbols > rather than beeing evaluated at run time. > > How would I have to rewrite this? > > Thanks > > Günther |
Dear Udo,
thanks for the hint, that will would only work though for a limited number of elements though, right? Is there another way of writing a literal, "late binding" array? Thanks Günther Udo Schneider wrote: > > self doThis: #(child parent) > self doThis: (Array with: child with: parent). > > Or if you do not need the variables: > > self doThis: #(1 2). > > CU, > > Udo > > > Guenther Schmidt wrote: > >> Hi, >> >> If write: >> >> |child parent| >> >> child := 1. >> parent := 2. >> >> self doThis: #(child parent) >> >> >> into a method the elements in the literal array are turned into >> symbols rather than beeing evaluated at run time. >> >> How would I have to rewrite this? >> >> Thanks >> >> Günther |
Guenther,
> thanks for the hint, that will would only work though for a limited > number of elements though, right? Correct. ArrayedCollection/Array provides #with:with:with:with:with: (sigh ;-) at max. This allows you to create an Array with max 5 elements. If you need more you can use ArrayedCollection>>withAll: if you already have a Collection. > Is there another way of writing a literal, "late binding" array? One alternative is to use a Stream to populate a collection. This frees you from taking care about the size and kind of collection: --->8------>8------>8------>8------>8------>8------>8------>8--- | mother father children | mother := 'Mother'. father := 'Father'. children := #('Brother' 'Sister'). family := ReadWriteStream on: Array new. family nextPut: mother; nextPut: father; nextPutAll: children. ^family contents --->8------>8------>8------>8------>8------>8------>8------>8--- In the example above you can add single elements using #nextPut:. If you have a collection you can either add the collection itself using #nextPut: or the elements of the collection using #nextPutAll:. In my experience it's often cleaner and more comfortable to access/populate a collection using streams. Collections/Streams in general are (IMHO) the most powerful things that differentiate Smalltalk from other languages. This the one feature where when not programming in Smalltalk I'm always thinking "this would be so easy in Smalltalk" ;-) I would suggest you try to get a copy of Kent Beck's "Smalltalk - Best Practice Patterns". It covers Stream and Collection patterns (and a lot more). I can provide you a German version "Smalltalk - Praxisnahe Gebrauchsmuster" if you want - however IMHO the english version is better. You might as well learn more here: http://www.iam.unibe.ch/~ducasse/FreeBooks/ByExample/13%20-%20Chapter%2011%20-%20Collections.pdf http://www.iam.unibe.ch/~ducasse/FreeBooks/ByExample/14%20-%20Chapter%2012%20-%20Strings.pdf http://www.iam.unibe.ch/~ducasse/FreeBooks/ByExample/15%20-%20Chapter%2013%20-%20Streams.pdf http://www.iam.unibe.ch/~ducasse/FreeBooks/Joy/8.pdf http://www.iam.unibe.ch/~ducasse/FreeBooks/Joy/9.pdf http://www.iam.unibe.ch/~ducasse/FreeBooks/Joy/10.pdf These books (and lot more) are online here (unfortunately not "Smalltalk - Best Practice Patterns"): http://www.iam.unibe.ch/~ducasse/FreeBooks.html Hope this helps, Udo Guenther Schmidt wrote: > Dear Udo, > > thanks for the hint, that will would only work though for a limited > number of elements though, right? > > Is there another way of writing a literal, "late binding" array? > > Thanks > > Günther > > Udo Schneider wrote: > >> > self doThis: #(child parent) >> self doThis: (Array with: child with: parent). >> >> Or if you do not need the variables: >> >> self doThis: #(1 2). >> >> CU, >> >> Udo >> >> >> Guenther Schmidt wrote: >> >>> Hi, >>> >>> If write: >>> >>> |child parent| >>> >>> child := 1. >>> parent := 2. >>> >>> self doThis: #(child parent) >>> >>> >>> into a method the elements in the literal array are turned into >>> symbols rather than beeing evaluated at run time. >>> >>> How would I have to rewrite this? >>> >>> Thanks >>> >>> Günther |
In reply to this post by Günther Schmidt
On Wed, 01 Dec 2004 19:12:19 +0100,
Guenther Schmidt <[hidden email]> wrote: > Dear Udo, > > thanks for the hint, that will would only work though for a limited > number of elements though, right? Since I'm a "Sch", too, allow me to jump in :-) You could cascade the addition like so: Array new add: one; add: two; add: threeDots. where you can insert add: messages until resource exhaustion. > > Is there another way of writing a literal, "late binding" array? It might be me, but the idea of a literal is to take advantage of the system's early binding capabilities. Making it "more late" is like asking for a more white version of a black pen. Maybe you are aiming as something like "early" storing dictionary keys and "lately" accessing their values? As in: | dict mykeys myvalues | dict := IdentityDictionary new at: #one put: 1; at: #two put: 2; at: #pi put: 3.14; yourself "literal array of dictionary keys" mykeys := #(one two) "'late bound' access" myvalues := mykeys collect: [ |eachKey| dict at: eachKey ] Just a wild guess at your intentions, probably way off target, and written in vim without checking the code ... But still with kind regards, s.sch. -- Stefan Schmiedl Approximity GmbH http://www.approximity.com Research & Development mailto:[hidden email] |
Stefan Schmiedl wrote:
> Array new > add: one; > add: two; > add: threeDots. I'm not sure whether this code fragment is valid (it isn't in Dolphin ST). #add: is normally part of an "extensible collection" protocol which is not implemented by all collection classes. CU, Udo |
On Wed, 01 Dec 2004 21:56:25 +0100,
Udo Schneider <[hidden email]> wrote: > Stefan Schmiedl wrote: >> Array new >> add: one; >> add: two; >> add: threeDots. > > I'm not sure whether this code fragment is valid (it isn't in Dolphin ST). > > #add: is normally part of an "extensible collection" protocol which is > not implemented by all collection classes. That's what I get for not testing ... it should have been OrderedCollection instead of Array. I'm sending myself off to bed without dinner now. s. |
Stefan Schmiedl wrote:
> I'm sending myself off to bed without dinner now. No need to. This problem (Arrays being non-extensible) led me to the solution with Arrays. I had a project which worked with Array with millions of elements. Using Arrays was a must due to perf. reasons. The first approach was creating a OrderedCollection and adding elements to it. Later convert it using #asArray. However this was slow and very memory intensive. Using the solution with Streams just worked perfectly. You don't even have to know the exact number of elements in advance ;-) CU, udo |
Free forum by Nabble | Edit this page |