Nicolas Cellier uploaded a new version of Collections to project The Trunk:
http://source.squeak.org/trunk/Collections-nice.136.mcz==================== Summary ====================
Name: Collections-nice.136
Author: nice
Time: 25 September 2009, 8:28:33 am
UUID: 814571f9-fe2a-4547-ae41-f0733a647794
Ancestors: Collections-nice.135
Fix from
http://bugs.squeak.org/view.php?id=7095RATIONALE:
Dictionary>>#collect: should preserve the keys
1) This is the ANSI specification
2) This is very logical
A Dictionary is a Bag of values indexed by unique, arbitrary keys.
OLD BEHAVIOUR CRITIC:
The old behaviour consisting in returning an OrderedCollection is wrong, at least for these reasons:
a Dictionary is unordered, and there is no reason to reassign keys (1 to: self size) with a random order...
BACKWARD COMPATIBILITY ISSUE:
Those relying on old behaviour should replace their code with something like:
aDictionary values asOrderedCollection collect: aBlock.
or:
(OrderedCollection newFrom: aDictionary) collect: aBlock.
=============== Diff against Collections-nice.135 ===============
Item was added:
+ ----- Method: Dictionary class>>newFromPairs: (in category 'instance creation') -----
+ newFromPairs: anArray
+
+ "Answer an instance of me associating (anArray at:i) to (anArray at: i+i)
+ for each odd i. anArray must have an even number of entries."
+
+ | newDictionary |
+
+ newDictionary := self new: (anArray size/2).
+ 1 to: (anArray size-1) by: 2 do: [ :i|
+ newDictionary at: (anArray at: i) put: (anArray at: i+1).
+ ].
+ ^ newDictionary
+
+ " Dictionary newFromPairs: {'Red' . Color red . 'Blue' . Color blue . 'Green' . Color green}. "!
Item was changed:
----- Method: Dictionary>>collect: (in category 'enumerating') -----
collect: aBlock
"Evaluate aBlock with each of my values as the argument. Collect the
resulting values into a collection that is like me. Answer with the new
collection."
| newCollection |
+ newCollection := self species new.
+ self associationsDo:[:each |
+ newCollection at: each key put: (aBlock value: each value).
+ ].
+ ^newCollection!
- newCollection := OrderedCollection new: self size.
- self do: [:each | newCollection add: (aBlock value: each)].
- ^ newCollection!