elect
10/17/2023, 4:22 PM.forEach {
val (k, v) = it.split(':')
meta[k.trim()] = v.trim()
}
I though I could make it more concise with associateTo
, but I still need two lines
.associateTo(meta) {
val (k, v) = it.split(':')
k.trim() to v.trim()
}
unless I'm missing something, I'd revert to the first versionephemient
10/17/2023, 4:40 PM.gropuingBy { it.substringBefore(':').trim() }
.aggregateTo(meta) { _, _, it, _ -> it.split(':')[1].trim() }
but that you have now is fine and clear enoughKlitos Kyriacou
10/17/2023, 5:35 PM.associateTo(meta) { it.split(':').map(String::trim).zipWithNext().single() }
This also checks that your value doesn't contain ':'
- not sure if you've dealt with that possibility. You might want .split(':', limit = 2)
.ephemient
10/17/2023, 5:44 PM:
in the string, in which case the code will failJacob
10/17/2023, 6:25 PMassociate
is a bit cleaner.
The other advantage of associateTo
is that it returns the map, but I can’t tell if you’d need that without seeing more contextelect
10/17/2023, 8:07 PMelect
10/17/2023, 8:09 PMephemient
10/17/2023, 8:14 PM.filter { ':' in it }
.associateTo(meta) {
it.substringBefore(':').trim() to
it.substringAfter(':').trim()
}
would be closer to that behaviormarcinmoskala
10/23/2023, 1:37 PMlet
in such situations
.associateTo(meta) {
it.split(':').let { (k, v) -> k.trim() to v.trim() }
}
ephemient
10/23/2023, 3:28 PM:
then the original code treats it as an empty value while split+bind will throw