Davio
11/30/2023, 8:42 AMmap
and flatMap
? Currently map
on Iterable always returns a List<T>
, but what if you applied map to a set and wanted a set as a result? There is mapTo
, but it is a bit cumbersome to use. What about mapToSet
or something? Then you could also have a proper initial size for the backing setDavio
11/30/2023, 8:44 AMmapTo
just adds every transformed item to the destination, so if you pass a new HashSet as a destination, it could be resized a couple of timesMervyn McCreight
11/30/2023, 8:49 AMmap
will keep the uniqueness of the items.
Your map function could e.g. map everything to the same value.ephemient
11/30/2023, 8:50 AMHashSet
sizing is additionally kind of tricky because even if the items don't collide, their hash codes may, or at least they might fall into the same buckets at certain sizesephemient
11/30/2023, 8:52 AMDavio
11/30/2023, 8:59 AMtoSet
also uses that heuristic in calculating the initial size for the setDavio
11/30/2023, 9:00 AMKlitos Kyriacou
11/30/2023, 9:09 AMKlitos Kyriacou
11/30/2023, 9:32 AMmapTo(HashSet.newHashSet(size)) { ... }
.phldavies
11/30/2023, 9:37 AMmapTo
and filterTo
etc, is the passed destination needs to be mutable, but usually I'm after an imutable result. I've taken to doing something like buildSet(size) { a.mapTo(this) { ... } }
which I could/should probably wrap into a fun Iterable<T>.mapToSet
helper...ephemient
11/30/2023, 9:40 AM.toSet()
doesn't bother wrapping in an immutable set, for the recordphldavies
11/30/2023, 9:41 AMphldavies
11/30/2023, 9:42 AMmapToSet(f) = mapTo(mutableSetOf(), f) as Set<T>
kinda thing would do the sameephemient
11/30/2023, 9:42 AM.mapTo()
e.g.
val output: Set<...> = list.mapTo(mutableSetOf()) { ... }
Klitos Kyriacou
11/30/2023, 9:55 AMfirst
can be called as collection.first()
or collection.first { it > 42 }
. It could be nice if toSet()
had an overload toSet(transformation: (T) -> R)
.ephemient
11/30/2023, 10:00 AMindexedToSet()
notNullToSet()
flattenToSet()
etc.
the existence of mapTo()
etc. is already enough for all common casesDavid Kubecka
11/30/2023, 11:50 AMmap
on the Set
doesn't simply return Set
, without any additional ceremony? I wouldn't mind if this was the default but of course it would be an unnecessary breaking change now. Still, I'm curious about the design decisions behind this.Davio
11/30/2023, 12:35 PMmap
it can make sense as uniqueness is not guaranteed of the returned elements and may lead to surprises, but Set<T>.filter()
also returns a List<T>
while filtering a set will always yield a (mathematical) set with the same or fewer elementsDavio
11/30/2023, 12:37 PMIterable<T>
returning a List<T>
ephemient
11/30/2023, 12:48 PMephemient
11/30/2023, 12:58 PMval a = sortedSetOf(Collator.getInstance(<http://Locale.US|Locale.US>), "\u00C1")
val b = a.toSet()
"A\u0301" in a
"A\u0301" !in b
David Kubecka
11/30/2023, 1:28 PMKlitos Kyriacou
11/30/2023, 2:06 PMDavio
11/30/2023, 2:30 PMDavio
11/30/2023, 2:34 PMDavid Kubecka
11/30/2023, 3:07 PMdifferent kinds of set behave differentlyEven though this might be true I still don't see why a map on a set shouldn't produce a set by default. Because in most code I've written or seen there's a sequence
set.map { ... }.toSet()
. Obviously I can write the code myself and is not such a big deal. I'm just interested why it's not the default for sets.David Kubecka
11/30/2023, 3:08 PM