https://kotlinlang.org logo
Title
d

Dominaezzz

06/06/2019, 7:29 PM
Anyone know of a collection version of
Map<K, V>.putIfAbsent(key: K, value: V)
?
putAll
overrides existing values sadly.
s

Shawn

06/06/2019, 7:33 PM
Would you mind clarifying what you mean by a “collection version?”
oh, I see, you want
putIfAbsent(other: Map<K, V>)
d

Dominaezzz

06/06/2019, 7:34 PM
I guess something like
Map<K, V>.putIfAbsent(other: Map<K, V>)
?
lol
yeah
s

Shawn

06/06/2019, 7:35 PM
you could just reverse the order of
putAll
, couldn’t you?
d

Dominaezzz

06/06/2019, 7:36 PM
Hmmm, if both were mutable.
I guess I need
MutableMap<K, V>.putIfAbsent(other: Map<K, V>)
😅 .
s

Shawn

06/06/2019, 7:37 PM
you could maybe use
+
in the preferred order?
d

Dominaezzz

06/06/2019, 7:39 PM
I need to do it multiple times to build a big map. I'd have to use
var
😅.
s

Shawn

06/06/2019, 7:39 PM
gotcha
here’s a somewhat cursed idea - what if you
fold
over a collection of all your maps
or maybe
reduce
into a mutableMap or something
d

Dominaezzz

06/06/2019, 7:41 PM
I'm more or less doing that already. I might be trying to hard to shrink this code.
Am I trying too hard?
s

Shawn

06/06/2019, 7:48 PM
¯\_(ツ)_/¯ hm
I couldn’t tell you from a quick glance, being entirely honest
d

Dominaezzz

06/06/2019, 7:49 PM
Ha okay. Thanks for helping anyway!
👍 1