On 5 January 2013 12:21, Ben Coman <
[hidden email]> wrote:
> Igor Stasenko wrote:
>>
>> On 5 January 2013 06:06, Ben Coman <
[hidden email]> 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}
>>
>>
>
> Thanks Igor. That is useful. So I ended up with the following to gather
> elements that only occur once (probably should have included that in the
> original scope)
> (#( 7 8 1 1 2 2 2 2 3 3 3 3 3 3 ) asBag sortedElements select: [ :asn | asn
> value = 1 ] ) collect: [ :asn | asn key ].
>
check the Bag protocol, there could be easier way to do it.
--
Best regards,
Igor Stasenko.