On 5 January 2013 06:06, Ben Coman <[email protected]> wrote:
>
> Is there a more elegant way to increment the value of a dictionary entry
> than what I have done in "this line" below?
>
> elements := #( 1 1 2 2 2 2 3 3 3 3 3 3) asOrderedCollection.
> elementCounter := Dictionary new.
> elements do: [ :key | elementCounter at: key put: (elementCounter at: key
> ifAbsent: [ 0 ]) + 1 ]. "this line"
> elementCounter inspect. "--> a Dictionary(1->2 2->4 3->6 )"
>
why you don't use bag?
#( 1 1 2 2 2 2 3 3 3 3 3 3) asBag sortedCounts
{6->3. 4->2. 2->1}
> cheers -ben
>
--
Best regards,
Igor Stasenko.