[squeak-dev] Dictionary >> #at:ifAbsentPut:ifPresentPut:
Levente Uzonyi
leves at caesar.elte.hu
Fri Apr 30 20:42:20 UTC 2021
On Fri, 30 Apr 2021, Thiede, Christoph wrote:
>
> Hi all,
>
>
> recently I stumbled upon an extension method for Dictionary which *might* fit well into the Trunk:
>
>
> Dictionary >> #at: key ifAbsentPut: startBlock ifPresentPut: nextBlock
> "(c) 2020 Basti Kruck und Marcel Taeumel. :-)" (self includesKey: key) ifFalse: [
> ^ self at: key put: startBlock value]. ^ self
> at: key
> put: (nextBlock value: (self at: key))
>
> SeeĀ https://github.com/hpi-swa/vivide/blob/c4ac5ea562bd563358d52ee41c149a3fbcffd90e/repository/Vivide.package/Dictionary.extension/instance/at.ifAbsentPut.ifPresentPut..st.
>
>
> Usage:
>
> counter := counters at: key ifAbsentPut: [0] ifPresentPut: [:i | i + 1]
>
> What do you think, should we copy this into the Trunk? Or is it just fine to do something like this instead at the sender side:
>
> counter := counters at: key put: (counters at: key ifAbsent: [0] ifPresent: [:i | i + 1])
The alternative I would use is the following:
counter := counters at: key put: (counters at: key ifAbsent: [0]) + 1
And I think that is good enough. If wanted to have a special method for
that case, I would try to look up the key only once.
Levente
More information about the Squeak-dev
mailing list
|