Nir
12/01/2020, 8:55 PMmyMap[k] = myMap[k]!! + 1myMap[k]!! += 1modifyupdatefun 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)) }myMap[k] = myMap[k]!! + 1myMap.update(k) { it + 1}Marc Knaup
12/01/2020, 8:57 PMupdateNir
12/01/2020, 8:57 PMNir
12/01/2020, 8:57 PMgetValueNir
12/01/2020, 8:59 PMupdateOrThrowsMarc Knaup
12/01/2020, 8:59 PM[ ]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 PMmapMarc 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()Nir
12/01/2020, 9:07 PMephemient
12/01/2020, 9:08 PMephemient
12/01/2020, 9:09 PMmyMap[k] = myMap.getOrElse(k) { 0 } + 1Nir
12/01/2020, 9:10 PMephemient
12/01/2020, 9:10 PMNir
12/01/2020, 9:10 PMmapephemient
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#computeQuincy
12/01/2020, 9:13 PMephemient
12/01/2020, 9:13 PMMap#mergeNir
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 }Nir
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#withDefaultNir
12/01/2020, 9:17 PMNir
12/01/2020, 9:18 PMephemient
12/01/2020, 9:19 PM.get(k, default).getOrElse(k) { default }Nir
12/01/2020, 9:20 PMephemient
12/01/2020, 9:20 PM.withDefault()MapNir
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+=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