What's the nicest + efficient way to do a map of s...
# announcements
n
What's the nicest + efficient way to do a map of some collections into sets, and flatten into a union of those sets? Right now I'm thinking:
Copy code
val finalSet = whateverlist.fold(mutableSetOf()) { acc, element ->
    val individualSet = someCode(element)
    acc.apply { addAll(individualSet) }
}
I'd expect this to be a little faster than some of the other approaches (e.g. reduce with regular Set and
union
)
r
I'd think
someList.flatten().toSet()
would work well enough
you could throw in an
asSequence()
if you thought it was necessary
n
You're probably right
a very primitive benchmark confirms little difference, thanks for the suggestion
r
np 🙂
n
for some reason I didn't think of that, when I was doing map I was followuping up with reduce + union instead of simply flatten and toSet