val transform: (T) -> R = TODO("some transform here")
val src: Collection<T> = TODO("some collection here")
val dst: Set<R> = src.toSet { transform(it) }
This has the following advantages over the current method of writing map { transform(it) }.toSet().
Less code is required.
Improved processing efficiency.
In particular, I feel that the improvement in processing efficiency is beneficial because it eliminates the need to declare MutableSet as a temporary variable and initialize it with forEach.
The “problem” with this is that the created set is a mutable one afterwards. I was also looking for ways to “lock” a set that was initially created by such a mapping operation to make it immutable afterwards, without converting it to a new set via .toSet() afterwards.
You could achieve that by appending an as cast like in listOf("").mapTo(mutableSetOf()) { it } as Set<String>, but that simply does not look nice.