[comp.lang.smalltalk] sortedCollection in Digitalk/V

caw@munnari.oz.au (Chris Wright) (12/23/90)

I hope this is the right place to ask...
I've got a small application that uses sortedCollections. Each 
element of the sortedcollection is a three element array, and the 
sort block that I define when createing the collection looks like this
[:a :b | (a at:3) <= (b at:3)]
and it all works fine, sorting the collection as I add elements by the
values of the third element of each added array.
I run into a problem when I try to use select: collect: reject: &c,
and I get a walkback. I think (from looking with the debugger) that
this is because Smalltalk/V creates a new sortedcollection to hold
the answer, but this sortedcollection has the **default** sort block
[:a :b| a<=b], which of course makes no sense when comparing my
three element arrays.
I should have said at the start that I'm using Smalltalk/V for the Mac,
v 1.0 (waiting for 1.1).
I can work around this, but is there a better way, or am I stuck with
this behaviour??

thanks for any help..

chris

*************

chris wright -- st. vincen't hospital, melbourne           
             -- a guest on melb university system

tpermutt@eng.umd.edu (Thomas Permutt) (12/24/90)

In article <6340@munnari.oz.au> caw@munnari.oz.au (Chris Wright) writes:
>I hope this is the right place to ask...
Certainly.
>I run into a problem when I try to use select: collect: reject: &c,
>and I get a walkback. I think (from looking with the debugger) that
>this is because Smalltalk/V creates a new sortedcollection to hold
>the answer, but this sortedcollection has the **default** sort block
>[:a :b| a<=b], which of course makes no sense when comparing my
>three element arrays.

I had a similar problem creating a Matrix class:  aMatrix collect: [...]
should answer a Matrix of the same shape; the matrix dimensions were
instance variables, like your sortBlock.  I did the following clumsy thing,
which I think nevertheless qualifies as a fix rather than a work-around.

Add a variable SortBlock to CLASS SortedCollection.  Modify the method new
or new: or whatever in class SortedCollection to set the sortBlock for the
instance to the value of the shared class variable SortBlock, after setting
that if necessary to the argument of the creation message.  Modify the
message species to set the class variable to the sortBlock of the receiver.

In other words, the class remembers the sortBlock of some relevant instance:
usually the most recently created one, but most importantly, the one which
was sent the "species" message by collect: etc., so that a new instance
can be created not only with the same class, but with the same sortBlock as
well.

If this is not clear I can mail you the exact code.

As I said, I find this clumsy, and if someone has a better idea I would like
to hear it.

I use Smalltalk/V on a PC, but I don't think it matters.