reactormonk
11/28/2024, 12:51 PMephemient
11/28/2024, 1:00 PMephemient
11/28/2024, 1:04 PMbuildMap {
for (map in listOfMaps) {
map.mapValuesTo(this) { (key, value) ->
if (contains(key)) mergeValues(getValue(key), value) else value
}
}
}
reactormonk
11/28/2024, 1:19 PMCardMessageLinksDto(it.fold(mapOf<Uuid, CardMessageLinkDto>()) { acc, links ->
links.cardMessageLinks.fold(acc) { newLinks, link ->
val current = newLinks[link.uuid]
if (current != null) {
if (current.updated != null
&& link.updated != null
&& <http://current.updated.at|current.updated.at> < <http://link.updated.at|link.updated.at>) {
newLinks + (link.uuid to link)
} else
newLinks
} else {
newLinks + (link.uuid to link)
}
}
}.values.toList())
ephemient
11/28/2024, 1:24 PMlistOfMaps.asSequence()
.flatMap { it.entries }
.groupingBy { it.key }
.aggregate { _, acc, (_, value), first -> if (first) value else mergeValues(acc, value) }
could look a bit more functionalShawn
11/28/2024, 9:31 PMmapList.stream()
.flatMap(it -> it.entrySet().stream())
.collect(toMap(
Map.Entry::getKey,
Map.Entry::getValue,
Foo::mergingFunction));
ephemient
11/28/2024, 10:07 PMlistOfMaps
.flatMap { it.entries }
.groupBy { it.key }
.mapValues { entries ->
val values = entries.map { it.value }
values.reduce { value1, value2 -> mergeValues(value1, value2) }
}
is pretty readable. and still O(N) in total entries as opposed to reactormonk's map + pair
solution turning an O(N) problem into an O(N^2) solution