dave08
04/04/2024, 3:49 PMbuildSet { foo.mapTo(this) { ... } }
instead of foo.map { ... }.toSet()
to avoid iterating twice... could the stdlib maybe provide mapToSet { ... }
?Joffrey
04/04/2024, 3:51 PMjw
04/04/2024, 3:52 PMmapTo(HashSet()) { .. }
because it returns the mutable type?dave08
04/04/2024, 3:53 PMdave08
04/04/2024, 3:53 PMdave08
04/04/2024, 3:55 PMmapTo(HashSet()) { .. }
, that's probably why that issue didn't really get upvoted...jw
04/04/2024, 3:56 PMjw
04/04/2024, 3:57 PMdave08
04/04/2024, 4:01 PMbecause usually it quickly flows into something else or returns where it's exposed as a read-only setYeah, we could make do in some cases like you said, but I'd always rather not rely on that, since with a little unconscious refactoring one could end up making mistakes, especially with IDE refactoring features and AI...
dave08
04/04/2024, 4:01 PMJoffrey
04/04/2024, 4:02 PMmapToSet
, especially when mapping elements from a Set
. It just feels more natural when I do need to return a set. Admittedly this doesn't come up very often, but every time it does, it feels awkward.jw
04/04/2024, 4:03 PMjw
04/04/2024, 4:04 PM.map { .. }.toSet()
do the right thing, but smarter compilers are slower.dave08
04/04/2024, 4:04 PMAdmittedly this doesn't come up very often,I came across quite a few recently when I needed to subtract keys from a map and the IDE tells you sets are more efficient... but there are a bunch of other places too... and using buildSet is pretty ugly there...
jw
04/04/2024, 4:05 PMjw
04/04/2024, 4:06 PMJoffrey
04/04/2024, 4:06 PMdave08
04/04/2024, 4:07 PMmap { }
to get the key anyways... so might as well do it to a set? But interesting... so that IDE comment is wrong?jw
04/04/2024, 4:08 PMasdf asdf
04/04/2024, 5:38 PMfoo
to a sequence seems like it would solve the double-iteration problem, but I guess that could be considered too much overheadrnett
04/05/2024, 4:18 PM.map { }.toImmutableList()
(or toImmutableSet()
), which has a lot of overlap with this problemThomas
04/23/2024, 3:11 AMtoList, toMutableList, toSet, toMutableSet, toHashSet, toMap, toMap, toCollection
) has transform: (T) -> R
as argument, we could simply introduce that.
I think foo.toSet{ ... }
is quite readable