Nir
12/01/2020, 8:55 PMmyMap[k] = myMap[k]!! + 1
or similar, to update the map. Kotlin doesn't seem to allow things like myMap[k]!! += 1
either. Wouldn't it be useful to have a modify
or update
function? Is there one already that I missed? It clarifies intent and avoids the repetition of the key:
fun MutableMap<K, V>.update(k: K, transform: (V) -> V) { this[k] = transform(getValue(k)) }
fun MutableMap<K, V>.update(k: K, default: V, transform: (V) -> V) { this[k] = transform(getOrDefault(k, default)) }
Usage would mean that instead of myMap[k] = myMap[k]!! + 1
you'd be able to write myMap.update(k) { it + 1}
Marc Knaup
12/01/2020, 8:57 PMupdate
do if the key doesn’t exist?
Throwing seems an easy to miss error 🤔Nir
12/01/2020, 8:57 PMNir
12/01/2020, 8:57 PMgetValue
also throwsNir
12/01/2020, 8:59 PMupdateOrThrows
if you wanted, seems overkill to me but ehMarc Knaup
12/01/2020, 8:59 PM[ ]
or getOr…
.Marc Knaup
12/01/2020, 9:00 PMNir
12/01/2020, 9:00 PMNir
12/01/2020, 9:00 PMMarc Knaup
12/01/2020, 9:01 PMNir
12/01/2020, 9:01 PMNir
12/01/2020, 9:01 PMMarc Knaup
12/01/2020, 9:01 PMMarc Knaup
12/01/2020, 9:02 PMNir
12/01/2020, 9:02 PMMarc Knaup
12/01/2020, 9:02 PMNir
12/01/2020, 9:02 PMMarc Knaup
12/01/2020, 9:03 PMNir
12/01/2020, 9:03 PMMarc Knaup
12/01/2020, 9:03 PMNir
12/01/2020, 9:04 PMMarc Knaup
12/01/2020, 9:04 PMNir
12/01/2020, 9:05 PMMarc Knaup
12/01/2020, 9:06 PMNir
12/01/2020, 9:06 PMNir
12/01/2020, 9:06 PMmap
quite frequently, yeah?Marc Knaup
12/01/2020, 9:06 PMNir
12/01/2020, 9:07 PMNir
12/01/2020, 9:07 PMMarc Knaup
12/01/2020, 9:07 PM.map()
returns copies and doesn’t mutate anything.Nir
12/01/2020, 9:07 PMephemient
12/01/2020, 9:08 PMephemient
12/01/2020, 9:09 PMmyMap[k] = myMap.getOrElse(k) { 0 } + 1
is very onerousNir
12/01/2020, 9:10 PMephemient
12/01/2020, 9:10 PMNir
12/01/2020, 9:10 PMmap
ephemient
12/01/2020, 9:11 PMephemient
12/01/2020, 9:11 PMMarc Knaup
12/01/2020, 9:12 PMit’s not very onerous to just use a for loop instead of mapActually it is, for example when you use chaining. Function chaining is one of Kotlin’s strong points.
Nir
12/01/2020, 9:13 PMQuincy
12/01/2020, 9:13 PMMap#compute
function is for isn't it?Quincy
12/01/2020, 9:13 PMephemient
12/01/2020, 9:13 PMMap#merge
, depending on your use caseNir
12/01/2020, 9:13 PMNir
12/01/2020, 9:14 PMephemient
12/01/2020, 9:14 PMmyMap.merge(k, 1) { old, new -> old + new }
will put 1 if absent, or increment by 1Nir
12/01/2020, 9:14 PMfun MutableMap<K, V>.update(k: K, transform: (V?) -> V) { this[k] = transform(this[k])) }
Nir
12/01/2020, 9:14 PMephemient
12/01/2020, 9:15 PMQuincy
12/01/2020, 9:16 PMMap#withDefault
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/with-default.htmlNir
12/01/2020, 9:17 PMNir
12/01/2020, 9:18 PMephemient
12/01/2020, 9:19 PM.get(k, default)
- which is the same as Kotlin's .getOrElse(k) { default }
Nir
12/01/2020, 9:20 PMephemient
12/01/2020, 9:20 PM.withDefault()
returned a subtype of Map
which guaranteed non-null indexingNir
12/01/2020, 9:20 PMIn [1]: x = {"helo": 1}
In [2]: x["helo"] += 1
In [3]: x
Out[3]: {'helo': 2}
Nir
12/01/2020, 9:20 PMNir
12/01/2020, 9:20 PMephemient
12/01/2020, 9:20 PMNir
12/01/2020, 9:20 PMNir
12/01/2020, 9:21 PMephemient
12/01/2020, 9:21 PM+=
. I mostly avoid that in Kotlin anyway, due to the confusion between var and mutable.Nir
12/01/2020, 9:22 PMMarc Knaup
12/01/2020, 9:22 PMNir
12/01/2020, 9:23 PMNir
12/01/2020, 9:24 PMMarc Knaup
12/01/2020, 9:24 PMNir
12/01/2020, 9:25 PMNir
12/01/2020, 9:25 PMMarc Knaup
12/01/2020, 9:25 PMNir
12/01/2020, 9:27 PM