IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New Smalltalk Question
What is a "real" Smalltalk way to move some elements to another collection. As in, I need the elements of collection to be split into 2 subset: one stays, another goes to a different collection.

RIght now I do it like this:

|collToStay collToGo|

collToStay := coll select: [ : each | each stays].
collToGo := coll select: [ : each | each stays not].
coll := collToStay.
self sendForth: collToGo.

Is there a better way?
New Isn't there a reject message?
So you could do:

|collToGo|

collToGo := coll reject: [ : each | each stays].
coll := coll select: [ : each | each stays].
self sendForth: collToGo.

... but you're probably looking for something to send two collections two ways simultaneously, aren't you?

Wade.

"All around me are nothing but fakes
Come with me on the biggest fake of all!"

New Re: Isn't there a reject message?
yeah, but it's still 2 messages...
Would be more elegant to have a "split:" mesage: things that are removed get gathered in another collection.

New Yes
use #removeAllSuchThat: aBlock

Like most Smalltalk collection methods, it returns something useful. In this case, it removes all objects for which aBlock is true; it also returns a collection of what was removed

======================
|colToGo |

colToGo := coll removeAllSuchThat: [: each | each stays not]
======================

This is present in VisualWorks; I haven't checked other implemtations. If not available on your platform, I can post the code for the method

Take care,

Jay O'Connor

"Going places unmapped
to do things unplanned
to people unsuspecting"
New Yes... Yes! Yes!
:)

Thanks for the hint.
I am using VisualWorks non-commercial.
New So how's it done?
I can see the stripped members being sent back as the result of the function. The only way I can see that the function would strip the original collection is if it uses a become: method. So is that how it's done?
New I don't get it
It just loops through itself and anything that matches the block gets added to a temp collection to be returned and removed

Do you mean how do you remove from within a loop? How do you change a collection while iterating over the collection? The loop internal to the method actually sets up an loop over the indices of the collection. The element to be removed used removed using it's index (#removeByIndex: ) #removeByIndex: just goes from the given index to end, scooting every element up one position and then cutting off the last element
Jay O'Connor

"Going places unmapped
to do things unplanned
to people unsuspecting"
New Never mind...
...been studying too many functional languages, and for some reason I keep thinking that collections are immutable objects in ST. Must go back and study Squeak some more.

BTW, the message that you gave for VW doesn't seem to be standard in Squeak.
New heh heh heh
I have that problem in Icon, too. Collections and variables are generally intuitively mutable wherever you put them :-). Sometimes makes for very very elegant code.

Wade.

"All around me are nothing but fakes
Come with me on the biggest fake of all!"

New Standard or not?
Did you look in List or OrderedCollection? That's where it is.
New Not aware
Not a ST expert by any means - but a couple of obvservations. First, you could save yourself a local variable if you want to just reallocate the coll object in place (see bellow). Second, you have to be careful that the "coll" variable is shared in all usages - the coll variable points to a new object as a result of the operation, but any other variables that pointed to the previous collection object still point to the object originally pointed to by coll. IOW, you end up with three objects as a result of the operation - one of which will be garbage collected if there are no other references holding on to it.

The only other thought that comes to mind is that ST only allows one value to be returned by a function. So if you wanted two collections to be returned, you'd have to have return a collection that contained both collections. That would probably be the more correct ST solution since it doesn't touch the original collection in the logic (i.e. rely on a variable being invisibly passed to the method).

|collToGo|

collToGo := coll select: [ : each | each stays not].
coll := coll select: [ : each | each stays].
self sendForth: collToGo.
New Re: Not aware
coll is a member variable in the class instance. It never escapes the instance - no methods return it. I am aware of having 3 objects at some point, and that's one of the reasons I am looking for a better solution.
New Speaking of smalltalk
Is there a good book that would help me wrap my mind around it?

With all respect to smalltalk, everytime I see code, I get about two steps into it and then my mind turns into a pretzel. My first language was Fortran. My first language at work was C. I've done some Turbo Pascal and more recently C++ and Java, but Smalltalk..... BONK
French Zombies are zapping me with lasers!
New Re: Speaking of smalltalk
My first language was Fortran.

Oh your poor thing! There's your problem for sure.

The syntax mind bender is a prejudice you've formed over time.

You're used to seeing stuff like HashMapAdd(map,key,value);
Which is a really sucky syntax - even worse are the way javap prints out
the prototypes as

void java.util.HashMap.add(java.lang.Object,java.lang.Object) and you have
no idea which argument which.

Smalltalk puts arguments within the selectors (method names basically).

dictionary at: aKey put: anObject

the method name is at:put: or as more commonly written #at:put:

Anyhow, the David N Smith book is pretty good in general. The Ken Howard book Developers Guide to Visualworks is really good if you're using Visualworks. The Art and Science of Smalltalk (forget the author offhand) is also quite decent.
New Re: Speaking of smalltalk
Try this link: [link|http://wiki.cs.uiuc.edu/VisualWorks/Joy+of+Smalltalk|http://wiki.cs.uiuc...of+Smalltalk]

I don't know if it's the best book, but it's online and it's free. I actually liked it.

But, when you want to wrap your mind around the relationship between Class and Object and Metaclass and Behavior.... You are on your own. Wrack your brain til you get it.
New Fortunately
But, when you want to wrap your mind around the relationship between Class and Object and Metaclass and Behavior.... You are on your own. Wrack your brain til you get it

That's more a curiosity for those inquisitive on 'how things work' then required knowledge for most programming
Jay O'Connor

"Going places unmapped
to do things unplanned
to people unsuspecting"
New I don't think _I_ could do without.
I really can't work if there is any magic involved ( above the level of hardware :) )
     Smalltalk Question - (Arkadiy) - (16)
         Isn't there a reject message? - (static) - (8)
             Re: Isn't there a reject message? - (Arkadiy) - (7)
                 Yes - (Fearless Freep) - (6)
                     Yes... Yes! Yes! - (Arkadiy)
                     So how's it done? - (ChrisR) - (4)
                         I don't get it - (Fearless Freep) - (3)
                             Never mind... - (ChrisR) - (2)
                                 heh heh heh - (static)
                                 Standard or not? - (Arkadiy)
         Not aware - (ChrisR) - (1)
             Re: Not aware - (Arkadiy)
         Speaking of smalltalk - (wharris2) - (4)
             Re: Speaking of smalltalk - (tuberculosis)
             Re: Speaking of smalltalk - (Arkadiy) - (2)
                 Fortunately - (Fearless Freep) - (1)
                     I don't think _I_ could do without. - (Arkadiy)

We sell COFFEE-flavored coffee.
164 ms