List Info

Thread: Add #atAll:, improve MappedCollection




Add #atAll:, improve MappedCollection
country flaguser name
Italy
2007-06-07 02:28:13
This patch adds an #atAll: method to the keyed collections
(i.e. 
Dictionary, MappedCollection, SequenceableCollection).  Note
that the 
Dictionary and MappedCollection version returns another
Dictionary, thus 
keeping the original keys:

   st> ((Dictionary from: {1->$a. 2->$b}) atAll:
#(2)) printNl; keys
   Dictionary (2->$b)
   Set (2 )

while the SequenceableCollection version returns another 
SequenceableCollection, thus reordering the keys:

   st> #($a $b) atAll: #(2)) printNl; keys
   ($b )
   Interval(1)

An interesting side effect is that repeated keys in the
keyCollection 
(e.g. "foo atAll: #(2 2)") also have a different
effect for Dictionaries 
(where they are discarded) and for SequenceableCollections
(where they 
cause the same element to appear multiple times).

Since I was looking at this weird class, which I just used
BTW, I redid 
some of the methods in MappedCollection.  I also added
#copyFrom:to: to 
it, which is useful because the "map" is often a
SequenceableCollection.

Paolo

_______________________________________________
help-smalltalk mailing list
help-smalltalkgnu.org

http://lists.gnu.org/mailman/listinfo/help-smalltalk

  
Re: Add #atAll:, improve MappedCollection
country flaguser name
United States
2007-06-07 02:51:03
Paolo Bonzini wrote:
> This patch adds an #atAll: method to the keyed
collections (i.e. 
> Dictionary, MappedCollection, SequenceableCollection).

Great!  I'll add that

aSeqCollect atAll: anInterval

is a powerfully expressive idiom enabled by this extension,
replacing 
all sorts of specialized collection methods, and I'm bound
to use it all 
over the place when I don't want to look those up (or they
aren't good 
enough).

-- 
Stephen Compall
"^aCollection allSatisfy: [:each | aCondition]" is
the one true idiom


_______________________________________________
help-smalltalk mailing list
help-smalltalkgnu.org

http://lists.gnu.org/mailman/listinfo/help-smalltalk

Re: Add #atAll:, improve MappedCollection
country flaguser name
Italy
2007-06-07 03:04:39
> Great!  I'll add that
> 
> aSeqCollect atAll: anInterval
> 
> is a powerfully expressive idiom

Fully agreed -- to the point that I was surprised of its
absence.

>  enabled by this extension, replacing 
> all sorts of specialized collection methods

What are you thinking of?

I should add that MappedCollection is also a powerful tool
with this 
respect, since basically

     MappedCollection collection: foo map: aCollection

is a lazy version of "foo atAll: aCollection". 
For Intervals, using a 
MappedCollection may save lots of memory.

An interesting example is that of matrices, implemented over
a single 
array with rows*cols elements:

     initialize
         rowSections := (0 to: rows - 1) collect: [ :n |
	     MappedCollection
	          collection: data
	          map: (n * cols + 1 to: (n + 1) * cols) ].
         columnSections := (1 to: cols) collect: [ :n |
	     MappedCollection
	          collection: data
	          map: (n to: rows * cols by: cols) ].

     at: n
         ^rowSections at: n

     columnAt: n
         ^columnSections at: n

     at: row at: column
         ^(self at: row) at: column

Paolo


_______________________________________________
help-smalltalk mailing list
help-smalltalkgnu.org

http://lists.gnu.org/mailman/listinfo/help-smalltalk

Re: Add #atAll:, improve MappedCollection
country flaguser name
United States
2007-06-07 03:20:07
On Thu, 2007-06-07 at 10:04 +0200, Paolo Bonzini wrote:
> >  enabled by this extension, replacing 
> > all sorts of specialized collection methods
> 
> What are you thinking of?

Mostly the methods that answer slices of varying kinds from
seq-collections.

> I should add that MappedCollection is also a powerful
tool with this 
> respect, since basically
> 
>      MappedCollection collection: foo map: aCollection
> 
> is a lazy version of "foo atAll:
aCollection".  For Intervals, using a 
> MappedCollection may save lots of memory.

I always get the order of these confused when I think about
MappedCollections, so I have to read the docs to refresh. 
I'll come up
with a mnemonic eventually, but for now blame
#collection:map: as a
selector.

-- 
;;; Stephen Compall ** http://scompall.no
candysw.com/blog **
"^aCollection allSatisfy: [:each | aCondition]" is
the true idiom

_______________________________________________
help-smalltalk mailing list
help-smalltalkgnu.org

http://lists.gnu.org/mailman/listinfo/help-smalltalk

Re: Add #atAll:, improve MappedCollection
country flaguser name
Italy
2007-06-07 04:15:23
> I always get the order of these confused when I think
about
> MappedCollections, so I have to read the docs to
refresh.  I'll come up
> with a mnemonic eventually, but for now blame
#collection:map: as a
> selector.

#on:map:? #on:through:?

Paolo


_______________________________________________
help-smalltalk mailing list
help-smalltalkgnu.org

http://lists.gnu.org/mailman/listinfo/help-smalltalk

[1-5]

about | contact  Other archives ( Real Estate discussion Medical topics )