I know this was already covered but to simplify the explination for the beginner:
Sort is automatic for Dictionaries.
| dict | dict := Dictionary new. dict at: '3c' put: 'object 3c'. dict at: '1a' put: 'object 1a'. dict at: '2b' put: 'object 2b'. dict
Evaluating the code with "Print It" will respond: a Dictionary('1a'->'object 1a' '2b'->'object 2a' '3c'->'object 3a' )
----------------------
A useful capability of Dictionaries is merging files by key. Add old objects then overwriting with new objects and sorting is automatic.
| dict | dict := Dictionary new. dict at: '3c' put: 'object 3c'. dict at: '1a' put: 'object 1a'. dict at: '3c' put: 'new object 3c'. dict
a Dictionary('1a'->'object 1a' '3c'->'new object 3c' )
Opps! An early morning typo.
The correct sorted answer is:
a Dictionary('1a'->'object 1a' '2b'->'object 2b' '3c'->'object 3c' )
On Wed, Aug 26, 2009 at 3:58 AM, Overcomer Man overcomer.man@gmail.comwrote:
I know this was already covered but to simplify the explination for the beginner:
Sort is automatic for Dictionaries.
| dict | dict := Dictionary new. dict at: '3c' put: 'object 3c'. dict at: '1a' put: 'object 1a'. dict at: '2b' put: 'object 2b'. dict
Evaluating the code with "Print It" will respond: a Dictionary('1a'->'object 1a' '2b'->'object 2a' '3c'->'object 3a' )
A useful capability of Dictionaries is merging files by key. Add old objects then overwriting with new objects and sorting is automatic.
| dict | dict := Dictionary new. dict at: '3c' put: 'object 3c'. dict at: '1a' put: 'object 1a'. dict at: '3c' put: 'new object 3c'. dict
a Dictionary('1a'->'object 1a' '3c'->'new object 3c' )
On 26.08.2009, at 12:58, Overcomer Man wrote:
I know this was already covered but to simplify the explination for the beginner:
Sort is automatic for Dictionaries.
This is over-simplifying. *Printing* of dictionaries sorts automatically. But not regular iteration.
- Bert -
"Bert" == Bert Freudenberg bert@freudenbergs.de writes:
Bert> On 26.08.2009, at 12:58, Overcomer Man wrote:
I know this was already covered but to simplify the explination for the beginner:
Sort is automatic for Dictionaries.
Bert> This is over-simplifying. *Printing* of dictionaries sorts automatically. But Bert> not regular iteration.
And this is because #printOn: contains sorting code:
"Collection >> #printOn:" printOn: aStream "Append a sequence of characters that identify the receiver to aStream."
self printNameOn: aStream. self printElementsOn: aStream
which further calls:
"Dictionary >> #printElementsOn:" printElementsOn: aStream aStream nextPut: $(. self size > 100 ifTrue: [aStream nextPutAll: 'size '. self size printOn: aStream] ifFalse: [self keysSortedSafely do: [:key | aStream print: key; nextPutAll: '->'; print: (self at: key); space]]. aStream nextPut: $)
Note the "self keysSortedSafely". This is another strategy for walking a dictionary in key order.
As we were discussing this issue last night at the Portland Smalltalk meeting after-meeting dinner, whether it is faster to grab the associations to sort them, or to grab the keys to sort them first then go look up the values, depends on the storage of a Dictionary. And I suspect that since the Squeak implementation of Dictionary already has the associations and just needs to spit them out, that's probably going to be faster than going back to look them up one by one, as the above code does.
On 26.08.2009, at 16:50, Randal L. Schwartz wrote:
As we were discussing this issue last night at the Portland Smalltalk meeting after-meeting dinner, whether it is faster to grab the associations to sort them, or to grab the keys to sort them first then go look up the values, depends on the storage of a Dictionary. And I suspect that since the Squeak implementation of Dictionary already has the associations and just needs to spit them out, that's probably going to be faster than going back to look them up one by one, as the above code does.
This is leaving newbie-territory, but don't speculate about performance, measure instead:
| dict | dict := Dictionary new. 100000 timesRepeat: [dict at: 1000000 atRandom put: 'dummy']. { [dict keys asSortedCollection do: [:key | | value | value := dict at: key]] timeToRun. [dict associations asSortedCollection do: [:assoc | | key value | key := assoc key. value := assoc value]] timeToRun. }
... and this would give the advantage to using #keys.
- Bert -
"Bert" == Bert Freudenberg bert@freudenbergs.de writes:
Bert> ... and this would give the advantage to using #keys.
Fascinating. I suppose it's all those extra calls to extract the key from the association during the sort.
In this case, it's the inverse of the Schwartzian Transform. :)
On 26.08.2009, at 17:31, Randal L. Schwartz wrote:
"Bert" == Bert Freudenberg bert@freudenbergs.de writes:
Bert> ... and this would give the advantage to using #keys.
Fascinating. I suppose it's all those extra calls to extract the key from the association during the sort.
You're speculating again ;)
In this case, it's the inverse of the Schwartzian Transform. :)
-- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 merlyn@stonehenge.com URL:http://www.stonehenge.com/merlyn/ Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc. See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
- Bert -
beginners@lists.squeakfoundation.org