Provide distinctBy(keySelector) method for collections and sequences.

Implement distinct() and toMutableSet() for sequences.
Breaking change: distinct() now returns List instead of Set.

#KT-5834 Fixed
#KT-6063 Fixed
This commit is contained in:
Ilya Gorbunov
2015-04-27 16:36:53 +03:00
parent 46d91b2606
commit 6a3cb0eff8
5 changed files with 344 additions and 39 deletions
+246 -30
View File
@@ -11,73 +11,269 @@ import java.util.*
import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js
/**
* Returns a set containing all distinct elements from the given collection.
* Returns a list containing only distinct elements from the given collection.
* The elements in the resulting list are in the same order as they were in the source collection.
*/
public fun <T> Array<out T>.distinct(): Set<T> {
return this.toMutableSet()
public fun <T> Array<out T>.distinct(): List<T> {
return this.toMutableSet().toList()
}
/**
* Returns a set containing all distinct elements from the given collection.
* Returns a list containing only distinct elements from the given collection.
* The elements in the resulting list are in the same order as they were in the source collection.
*/
public fun BooleanArray.distinct(): Set<Boolean> {
return this.toMutableSet()
public fun BooleanArray.distinct(): List<Boolean> {
return this.toMutableSet().toList()
}
/**
* Returns a set containing all distinct elements from the given collection.
* Returns a list containing only distinct elements from the given collection.
* The elements in the resulting list are in the same order as they were in the source collection.
*/
public fun ByteArray.distinct(): Set<Byte> {
return this.toMutableSet()
public fun ByteArray.distinct(): List<Byte> {
return this.toMutableSet().toList()
}
/**
* Returns a set containing all distinct elements from the given collection.
* Returns a list containing only distinct elements from the given collection.
* The elements in the resulting list are in the same order as they were in the source collection.
*/
public fun CharArray.distinct(): Set<Char> {
return this.toMutableSet()
public fun CharArray.distinct(): List<Char> {
return this.toMutableSet().toList()
}
/**
* Returns a set containing all distinct elements from the given collection.
* Returns a list containing only distinct elements from the given collection.
* The elements in the resulting list are in the same order as they were in the source collection.
*/
public fun DoubleArray.distinct(): Set<Double> {
return this.toMutableSet()
public fun DoubleArray.distinct(): List<Double> {
return this.toMutableSet().toList()
}
/**
* Returns a set containing all distinct elements from the given collection.
* Returns a list containing only distinct elements from the given collection.
* The elements in the resulting list are in the same order as they were in the source collection.
*/
public fun FloatArray.distinct(): Set<Float> {
return this.toMutableSet()
public fun FloatArray.distinct(): List<Float> {
return this.toMutableSet().toList()
}
/**
* Returns a set containing all distinct elements from the given collection.
* Returns a list containing only distinct elements from the given collection.
* The elements in the resulting list are in the same order as they were in the source collection.
*/
public fun IntArray.distinct(): Set<Int> {
return this.toMutableSet()
public fun IntArray.distinct(): List<Int> {
return this.toMutableSet().toList()
}
/**
* Returns a set containing all distinct elements from the given collection.
* Returns a list containing only distinct elements from the given collection.
* The elements in the resulting list are in the same order as they were in the source collection.
*/
public fun LongArray.distinct(): Set<Long> {
return this.toMutableSet()
public fun LongArray.distinct(): List<Long> {
return this.toMutableSet().toList()
}
/**
* Returns a set containing all distinct elements from the given collection.
* Returns a list containing only distinct elements from the given collection.
* The elements in the resulting list are in the same order as they were in the source collection.
*/
public fun ShortArray.distinct(): Set<Short> {
return this.toMutableSet()
public fun ShortArray.distinct(): List<Short> {
return this.toMutableSet().toList()
}
/**
* Returns a set containing all distinct elements from the given collection.
* Returns a list containing only distinct elements from the given collection.
* The elements in the resulting list are in the same order as they were in the source collection.
*/
public fun <T> Iterable<T>.distinct(): Set<T> {
return this.toMutableSet()
public fun <T> Iterable<T>.distinct(): List<T> {
return this.toMutableSet().toList()
}
/**
* Returns a sequence containing only distinct elements from the given sequence.
* The elements in the resulting sequence are in the same order as they were in the source sequence.
*/
public fun <T> Sequence<T>.distinct(): Sequence<T> {
return this.distinctBy { it }
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns a stream containing only distinct elements from the given stream.
* The elements in the resulting stream are in the same order as they were in the source stream.
*/
public fun <T> Stream<T>.distinct(): Stream<T> {
return this.distinctBy { it }
}
/**
* Returns a list containing only distinct elements from the given collection according to the [keySelector].
* The elements in the resulting list are in the same order as they were in the source collection.
*/
public inline fun <T, K> Array<out T>.distinctBy(keySelector: (T) -> K): List<T> {
val set = HashSet<K>()
val list = ArrayList<T>()
for (e in this) {
val key = keySelector(e)
if (set.add(key))
list.add(e)
}
return list
}
/**
* Returns a list containing only distinct elements from the given collection according to the [keySelector].
* The elements in the resulting list are in the same order as they were in the source collection.
*/
public inline fun <K> BooleanArray.distinctBy(keySelector: (Boolean) -> K): List<Boolean> {
val set = HashSet<K>()
val list = ArrayList<Boolean>()
for (e in this) {
val key = keySelector(e)
if (set.add(key))
list.add(e)
}
return list
}
/**
* Returns a list containing only distinct elements from the given collection according to the [keySelector].
* The elements in the resulting list are in the same order as they were in the source collection.
*/
public inline fun <K> ByteArray.distinctBy(keySelector: (Byte) -> K): List<Byte> {
val set = HashSet<K>()
val list = ArrayList<Byte>()
for (e in this) {
val key = keySelector(e)
if (set.add(key))
list.add(e)
}
return list
}
/**
* Returns a list containing only distinct elements from the given collection according to the [keySelector].
* The elements in the resulting list are in the same order as they were in the source collection.
*/
public inline fun <K> CharArray.distinctBy(keySelector: (Char) -> K): List<Char> {
val set = HashSet<K>()
val list = ArrayList<Char>()
for (e in this) {
val key = keySelector(e)
if (set.add(key))
list.add(e)
}
return list
}
/**
* Returns a list containing only distinct elements from the given collection according to the [keySelector].
* The elements in the resulting list are in the same order as they were in the source collection.
*/
public inline fun <K> DoubleArray.distinctBy(keySelector: (Double) -> K): List<Double> {
val set = HashSet<K>()
val list = ArrayList<Double>()
for (e in this) {
val key = keySelector(e)
if (set.add(key))
list.add(e)
}
return list
}
/**
* Returns a list containing only distinct elements from the given collection according to the [keySelector].
* The elements in the resulting list are in the same order as they were in the source collection.
*/
public inline fun <K> FloatArray.distinctBy(keySelector: (Float) -> K): List<Float> {
val set = HashSet<K>()
val list = ArrayList<Float>()
for (e in this) {
val key = keySelector(e)
if (set.add(key))
list.add(e)
}
return list
}
/**
* Returns a list containing only distinct elements from the given collection according to the [keySelector].
* The elements in the resulting list are in the same order as they were in the source collection.
*/
public inline fun <K> IntArray.distinctBy(keySelector: (Int) -> K): List<Int> {
val set = HashSet<K>()
val list = ArrayList<Int>()
for (e in this) {
val key = keySelector(e)
if (set.add(key))
list.add(e)
}
return list
}
/**
* Returns a list containing only distinct elements from the given collection according to the [keySelector].
* The elements in the resulting list are in the same order as they were in the source collection.
*/
public inline fun <K> LongArray.distinctBy(keySelector: (Long) -> K): List<Long> {
val set = HashSet<K>()
val list = ArrayList<Long>()
for (e in this) {
val key = keySelector(e)
if (set.add(key))
list.add(e)
}
return list
}
/**
* Returns a list containing only distinct elements from the given collection according to the [keySelector].
* The elements in the resulting list are in the same order as they were in the source collection.
*/
public inline fun <K> ShortArray.distinctBy(keySelector: (Short) -> K): List<Short> {
val set = HashSet<K>()
val list = ArrayList<Short>()
for (e in this) {
val key = keySelector(e)
if (set.add(key))
list.add(e)
}
return list
}
/**
* Returns a list containing only distinct elements from the given collection according to the [keySelector].
* The elements in the resulting list are in the same order as they were in the source collection.
*/
public inline fun <T, K> Iterable<T>.distinctBy(keySelector: (T) -> K): List<T> {
val set = HashSet<K>()
val list = ArrayList<T>()
for (e in this) {
val key = keySelector(e)
if (set.add(key))
list.add(e)
}
return list
}
/**
* Returns a sequence containing only distinct elements from the given sequence according to the [keySelector].
* The elements in the resulting sequence are in the same order as they were in the source sequence.
*/
public fun <T, K> Sequence<T>.distinctBy(keySelector: (T) -> K): Sequence<T> {
return DistinctSequence(this, keySelector)
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns a stream containing only distinct elements from the given stream according to the [keySelector].
* The elements in the resulting stream are in the same order as they were in the source stream.
*/
public fun <T, K> Stream<T>.distinctBy(keySelector: (T) -> K): Stream<T> {
return DistinctStream(this, keySelector)
}
/**
@@ -351,6 +547,26 @@ public fun <T> Iterable<T>.toMutableSet(): MutableSet<T> {
}
}
/**
* Returns a mutable set containing all distinct elements from the given sequence.
*/
public fun <T> Sequence<T>.toMutableSet(): MutableSet<T> {
val set = LinkedHashSet<T>()
for (item in this) set.add(item)
return set
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns a mutable set containing all distinct elements from the given stream.
*/
public fun <T> Stream<T>.toMutableSet(): MutableSet<T> {
val set = LinkedHashSet<T>()
for (item in this) set.add(item)
return set
}
/**
* Returns a set containing all distinct elements from both collections.
*/
@@ -1,7 +1,7 @@
package kotlin
import java.util.Enumeration
import java.util.NoSuchElementException
import java.util.*
import kotlin.support.AbstractIterator
deprecated("Use Sequence<T> instead.")
public interface Stream<out T> {
@@ -481,6 +481,32 @@ public class DropWhileSequence<T>(private val sequence: Sequence<T>,
}
}
deprecated("Use DistinctSequence instead and remove with streams.")
private class DistinctStream<T, K>(private val source: Stream<T>, private val keySelector : (T) -> K) : Stream<T> by DistinctSequence<T, K>(source.toSequence(), keySelector)
private class DistinctSequence<T, K>(private val source : Sequence<T>, private val keySelector : (T) -> K) : Sequence<T> {
override fun iterator(): Iterator<T> = DistinctIterator(source.iterator(), keySelector)
}
private class DistinctIterator<T, K>(private val source : Iterator<T>, private val keySelector : (T) -> K) : AbstractIterator<T>() {
private val observed = HashSet<K>()
override fun computeNext() {
while (source.hasNext()) {
val next = source.next()
val key = keySelector(next)
if (observed.add(key)) {
setNext(next)
return
}
}
done()
}
}
/**
* A sequence which repeatedly calls the specified [producer] function and returns its return values, until
* `null` is returned from [producer].