Any alternative to `List.zip(List).toMap(mutableMa...
# stdlib
s
Any alternative to
List.zip(List).toMap(mutableMapOf())
?
n
I guess I would use
List.zip(List).toMap().toMutableMap()
but that is mostly personal preference
btw: this would better be asked in #getting-started, this channel is for discussions about stdlib extensions/bugs/missing features
g
toMap(mutableMapOf()) is more efficient
I think it’s a good candidate for stdlib function, or with own function if performance is a concern, it’s pretty simple to write one and use case maybe not so common, but still I can imagine real cases for it
Something like this: https://pl.kotl.in/XNP4yGL9D
s
@gildor why inline?
Maybe
zipTo
?
g
I follow the same convention as all existing associateWith/associateBy functions from stdlib which create map from keys/values
inline as all similar stdlib functions (see for example List.zip)
probably zipTo also reasonable, but it would be confusing to have zipTo which returns map, and no zipTo with list as destination
but for own project of course you can use any naming, no need to worry too much about consistency with stdlib
n
I still fail to see the common use cases which would support adding this method. In all the cases I came across so far, I only needed an immutable result and thus was happy enough with zip+toMap.
g
well, the reason why we have toMap(destination) includes support of mutability too I agree, it’s not so common, but there are cases when you need mutability anyway
happy enough with zip+toMap
To be honest it’s not super efficient way, if you use it often, I would just write own function as one which I posted, just without destination
n
Understood. But the barrier of entry to stdlib is quite high per my understanding (for good reasons). So why is
List.zip(List).toMap(mutableMapOf())
not good enough? And yes: if that method would become a hotspot, I would try to optimize it along your code.
g
I’m not advocating for adding it to stdlib, just said that it may be reasonable to have something like this
if that method would become a hotspot
Not only because it’s hotspot, just a bit less GC pressure and clearly shows intent of the code