Add toSortedList operation for Streams
This commit is contained in:
committed by
Andrey Breslav
parent
9a7758bf4e
commit
5537ff5f3c
@@ -132,7 +132,7 @@ public fun <T> Iterable<T>.sortBy(comparator: Comparator<T>): List<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all elements, sorted by results of specified *order* function.
|
||||
* Returns a sorted list of all elements, ordered by results of specified *order* function.
|
||||
*/
|
||||
public inline fun <T, R : Comparable<R>> Array<out T>.sortBy(order: (T) -> R): List<T> {
|
||||
val sortedList = toArrayList()
|
||||
@@ -142,7 +142,7 @@ public inline fun <T, R : Comparable<R>> Array<out T>.sortBy(order: (T) -> R): L
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all elements, sorted by results of specified *order* function.
|
||||
* Returns a sorted list of all elements, ordered by results of specified *order* function.
|
||||
*/
|
||||
public inline fun <T, R : Comparable<R>> Iterable<T>.sortBy(order: (T) -> R): List<T> {
|
||||
val sortedList = toArrayList()
|
||||
@@ -152,7 +152,7 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.sortBy(order: (T) -> R): Li
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements
|
||||
* Returns a sorted list of all elements, in descending order
|
||||
*/
|
||||
public fun <T : Comparable<T>> Iterable<T>.sortDescending(): List<T> {
|
||||
val sortedList = toArrayList()
|
||||
@@ -162,7 +162,7 @@ public fun <T : Comparable<T>> Iterable<T>.sortDescending(): List<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all elements, sorted by results of specified *order* function.
|
||||
* Returns a sorted list of all elements, in descending order by results of specified *order* function.
|
||||
*/
|
||||
public inline fun <T, R : Comparable<R>> Array<out T>.sortDescendingBy(order: (T) -> R): List<T> {
|
||||
val sortedList = toArrayList()
|
||||
@@ -172,7 +172,7 @@ public inline fun <T, R : Comparable<R>> Array<out T>.sortDescendingBy(order: (T
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all elements, sorted by results of specified *order* function.
|
||||
* Returns a sorted list of all elements, in descending order by results of specified *order* function.
|
||||
*/
|
||||
public inline fun <T, R : Comparable<R>> Iterable<T>.sortDescendingBy(order: (T) -> R): List<T> {
|
||||
val sortedList = toArrayList()
|
||||
@@ -181,3 +181,212 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.sortDescendingBy(order: (T)
|
||||
return sortedList
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements
|
||||
*/
|
||||
public fun <T : Comparable<T>> Array<out T>.toSortedList(): List<T> {
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList)
|
||||
return sortedList
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements
|
||||
*/
|
||||
public fun BooleanArray.toSortedList(): List<Boolean> {
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList)
|
||||
return sortedList
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements
|
||||
*/
|
||||
public fun ByteArray.toSortedList(): List<Byte> {
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList)
|
||||
return sortedList
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements
|
||||
*/
|
||||
public fun CharArray.toSortedList(): List<Char> {
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList)
|
||||
return sortedList
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements
|
||||
*/
|
||||
public fun DoubleArray.toSortedList(): List<Double> {
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList)
|
||||
return sortedList
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements
|
||||
*/
|
||||
public fun FloatArray.toSortedList(): List<Float> {
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList)
|
||||
return sortedList
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements
|
||||
*/
|
||||
public fun IntArray.toSortedList(): List<Int> {
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList)
|
||||
return sortedList
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements
|
||||
*/
|
||||
public fun LongArray.toSortedList(): List<Long> {
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList)
|
||||
return sortedList
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements
|
||||
*/
|
||||
public fun ShortArray.toSortedList(): List<Short> {
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList)
|
||||
return sortedList
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements
|
||||
*/
|
||||
public fun <T : Comparable<T>> Iterable<T>.toSortedList(): List<T> {
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList)
|
||||
return sortedList
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements
|
||||
*/
|
||||
public fun <T : Comparable<T>> Stream<T>.toSortedList(): List<T> {
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList)
|
||||
return sortedList
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements, ordered by results of specified *order* function.
|
||||
*/
|
||||
public fun <T, V : Comparable<V>> Array<out T>.toSortedListBy(order: (T) -> V): List<T> {
|
||||
val sortedList = toArrayList()
|
||||
val sortBy: Comparator<T> = comparator<T> {(x: T, y: T) -> order(x).compareTo(order(y)) }
|
||||
java.util.Collections.sort(sortedList, sortBy)
|
||||
return sortedList
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements, ordered by results of specified *order* function.
|
||||
*/
|
||||
public fun <V : Comparable<V>> BooleanArray.toSortedListBy(order: (Boolean) -> V): List<Boolean> {
|
||||
val sortedList = toArrayList()
|
||||
val sortBy: Comparator<Boolean> = comparator<Boolean> {(x: Boolean, y: Boolean) -> order(x).compareTo(order(y)) }
|
||||
java.util.Collections.sort(sortedList, sortBy)
|
||||
return sortedList
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements, ordered by results of specified *order* function.
|
||||
*/
|
||||
public fun <V : Comparable<V>> ByteArray.toSortedListBy(order: (Byte) -> V): List<Byte> {
|
||||
val sortedList = toArrayList()
|
||||
val sortBy: Comparator<Byte> = comparator<Byte> {(x: Byte, y: Byte) -> order(x).compareTo(order(y)) }
|
||||
java.util.Collections.sort(sortedList, sortBy)
|
||||
return sortedList
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements, ordered by results of specified *order* function.
|
||||
*/
|
||||
public fun <V : Comparable<V>> CharArray.toSortedListBy(order: (Char) -> V): List<Char> {
|
||||
val sortedList = toArrayList()
|
||||
val sortBy: Comparator<Char> = comparator<Char> {(x: Char, y: Char) -> order(x).compareTo(order(y)) }
|
||||
java.util.Collections.sort(sortedList, sortBy)
|
||||
return sortedList
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements, ordered by results of specified *order* function.
|
||||
*/
|
||||
public fun <V : Comparable<V>> DoubleArray.toSortedListBy(order: (Double) -> V): List<Double> {
|
||||
val sortedList = toArrayList()
|
||||
val sortBy: Comparator<Double> = comparator<Double> {(x: Double, y: Double) -> order(x).compareTo(order(y)) }
|
||||
java.util.Collections.sort(sortedList, sortBy)
|
||||
return sortedList
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements, ordered by results of specified *order* function.
|
||||
*/
|
||||
public fun <V : Comparable<V>> FloatArray.toSortedListBy(order: (Float) -> V): List<Float> {
|
||||
val sortedList = toArrayList()
|
||||
val sortBy: Comparator<Float> = comparator<Float> {(x: Float, y: Float) -> order(x).compareTo(order(y)) }
|
||||
java.util.Collections.sort(sortedList, sortBy)
|
||||
return sortedList
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements, ordered by results of specified *order* function.
|
||||
*/
|
||||
public fun <V : Comparable<V>> IntArray.toSortedListBy(order: (Int) -> V): List<Int> {
|
||||
val sortedList = toArrayList()
|
||||
val sortBy: Comparator<Int> = comparator<Int> {(x: Int, y: Int) -> order(x).compareTo(order(y)) }
|
||||
java.util.Collections.sort(sortedList, sortBy)
|
||||
return sortedList
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements, ordered by results of specified *order* function.
|
||||
*/
|
||||
public fun <V : Comparable<V>> LongArray.toSortedListBy(order: (Long) -> V): List<Long> {
|
||||
val sortedList = toArrayList()
|
||||
val sortBy: Comparator<Long> = comparator<Long> {(x: Long, y: Long) -> order(x).compareTo(order(y)) }
|
||||
java.util.Collections.sort(sortedList, sortBy)
|
||||
return sortedList
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements, ordered by results of specified *order* function.
|
||||
*/
|
||||
public fun <V : Comparable<V>> ShortArray.toSortedListBy(order: (Short) -> V): List<Short> {
|
||||
val sortedList = toArrayList()
|
||||
val sortBy: Comparator<Short> = comparator<Short> {(x: Short, y: Short) -> order(x).compareTo(order(y)) }
|
||||
java.util.Collections.sort(sortedList, sortBy)
|
||||
return sortedList
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements, ordered by results of specified *order* function.
|
||||
*/
|
||||
public fun <T, V : Comparable<V>> Iterable<T>.toSortedListBy(order: (T) -> V): List<T> {
|
||||
val sortedList = toArrayList()
|
||||
val sortBy: Comparator<T> = comparator<T> {(x: T, y: T) -> order(x).compareTo(order(y)) }
|
||||
java.util.Collections.sort(sortedList, sortBy)
|
||||
return sortedList
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements, ordered by results of specified *order* function.
|
||||
*/
|
||||
public fun <T, V : Comparable<V>> Stream<T>.toSortedListBy(order: (T) -> V): List<T> {
|
||||
val sortedList = toArrayList()
|
||||
val sortBy: Comparator<T> = comparator<T> {(x: T, y: T) -> order(x).compareTo(order(y)) }
|
||||
java.util.Collections.sort(sortedList, sortBy)
|
||||
return sortedList
|
||||
}
|
||||
|
||||
|
||||
@@ -581,90 +581,6 @@ public fun String.toSet(): Set<Char> {
|
||||
return toCollection(LinkedHashSet<Char>())
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements
|
||||
*/
|
||||
public fun <T : Comparable<T>> Array<out T>.toSortedList(): List<T> {
|
||||
return toArrayList().sort()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements
|
||||
*/
|
||||
public fun BooleanArray.toSortedList(): List<Boolean> {
|
||||
return toArrayList().sort()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements
|
||||
*/
|
||||
public fun ByteArray.toSortedList(): List<Byte> {
|
||||
return toArrayList().sort()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements
|
||||
*/
|
||||
public fun CharArray.toSortedList(): List<Char> {
|
||||
return toArrayList().sort()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements
|
||||
*/
|
||||
public fun DoubleArray.toSortedList(): List<Double> {
|
||||
return toArrayList().sort()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements
|
||||
*/
|
||||
public fun FloatArray.toSortedList(): List<Float> {
|
||||
return toArrayList().sort()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements
|
||||
*/
|
||||
public fun IntArray.toSortedList(): List<Int> {
|
||||
return toArrayList().sort()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements
|
||||
*/
|
||||
public fun LongArray.toSortedList(): List<Long> {
|
||||
return toArrayList().sort()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements
|
||||
*/
|
||||
public fun ShortArray.toSortedList(): List<Short> {
|
||||
return toArrayList().sort()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements
|
||||
*/
|
||||
public fun <T : Comparable<T>> Iterable<T>.toSortedList(): List<T> {
|
||||
return sort()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements
|
||||
*/
|
||||
public fun <T : Comparable<T>> Stream<T>.toSortedList(): List<T> {
|
||||
return toArrayList().sort()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements
|
||||
*/
|
||||
public fun String.toSortedList(): List<Char> {
|
||||
return toArrayList().sort()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a SortedSet of all elements
|
||||
*/
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
package kotlin
|
||||
|
||||
import kotlin.support.AbstractIterator
|
||||
import java.util.*
|
||||
|
||||
public trait Stream<out T> {
|
||||
public fun iterator(): Iterator<T>
|
||||
}
|
||||
|
||||
public fun <T> streamOf(vararg elements : T) : Stream<T> = elements.stream()
|
||||
public fun <T> streamOf(vararg elements: T): Stream<T> = elements.stream()
|
||||
|
||||
public class FilteringStream<T>(val stream: Stream<T>, val sendWhen: Boolean = true, val predicate: (T) -> Boolean) : Stream<T> {
|
||||
override fun iterator(): Iterator<T> = object : AbstractIterator<T>() {
|
||||
@@ -37,8 +38,8 @@ public class TransformingStream<T, R>(val stream: Stream<T>, val transformer: (T
|
||||
}
|
||||
}
|
||||
|
||||
class ZippingStream<T1, T2>(val stream1: Stream<T1>, val stream2: Stream<T2>) : Stream<Pair<T1,T2>> {
|
||||
override fun iterator(): Iterator<Pair<T1,T2>> = object : AbstractIterator<Pair<T1,T2>>() {
|
||||
class ZippingStream<T1, T2>(val stream1: Stream<T1>, val stream2: Stream<T2>) : Stream<Pair<T1, T2>> {
|
||||
override fun iterator(): Iterator<Pair<T1, T2>> = object : AbstractIterator<Pair<T1, T2>>() {
|
||||
val iterator1 = stream1.iterator()
|
||||
val iterator2 = stream2.iterator()
|
||||
override fun computeNext() {
|
||||
|
||||
@@ -45,15 +45,34 @@ fun ordering(): List<GenericFunction> {
|
||||
}
|
||||
|
||||
exclude(Streams)
|
||||
exclude(ArraysOfPrimitives) // TODO: resolve collision between inplace sort and this function
|
||||
exclude(ArraysOfPrimitives)
|
||||
exclude(ArraysOfObjects)
|
||||
exclude(Strings)
|
||||
}
|
||||
|
||||
templates add f("toSortedList()") {
|
||||
doc {
|
||||
"""
|
||||
Returns a sorted list of all elements
|
||||
"""
|
||||
}
|
||||
returns("List<T>")
|
||||
typeParam("T : Comparable<T>")
|
||||
body {
|
||||
"""
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList)
|
||||
return sortedList
|
||||
"""
|
||||
}
|
||||
|
||||
only(Streams, ArraysOfObjects, ArraysOfPrimitives, Iterables)
|
||||
}
|
||||
|
||||
templates add f("sortDescending()") {
|
||||
doc {
|
||||
"""
|
||||
Returns a sorted list of all elements
|
||||
Returns a sorted list of all elements, in descending order
|
||||
"""
|
||||
}
|
||||
returns("List<T>")
|
||||
@@ -68,7 +87,7 @@ fun ordering(): List<GenericFunction> {
|
||||
}
|
||||
|
||||
exclude(Streams)
|
||||
exclude(ArraysOfPrimitives) // TODO: resolve collision between inplace sort and this function
|
||||
exclude(ArraysOfPrimitives)
|
||||
exclude(ArraysOfObjects)
|
||||
exclude(Strings)
|
||||
}
|
||||
@@ -78,7 +97,7 @@ fun ordering(): List<GenericFunction> {
|
||||
|
||||
doc {
|
||||
"""
|
||||
Returns a list of all elements, sorted by results of specified *order* function.
|
||||
Returns a sorted list of all elements, ordered by results of specified *order* function.
|
||||
"""
|
||||
}
|
||||
returns("List<T>")
|
||||
@@ -97,12 +116,33 @@ fun ordering(): List<GenericFunction> {
|
||||
exclude(Strings)
|
||||
}
|
||||
|
||||
templates add f("toSortedListBy(order: (T) -> V)") {
|
||||
doc {
|
||||
"""
|
||||
Returns a sorted list of all elements, ordered by results of specified *order* function.
|
||||
"""
|
||||
}
|
||||
returns("List<T>")
|
||||
typeParam("T")
|
||||
typeParam("V : Comparable<V>")
|
||||
body {
|
||||
"""
|
||||
val sortedList = toArrayList()
|
||||
val sortBy: Comparator<T> = comparator<T> {(x: T, y: T) -> order(x).compareTo(order(y)) }
|
||||
java.util.Collections.sort(sortedList, sortBy)
|
||||
return sortedList
|
||||
"""
|
||||
}
|
||||
|
||||
only(Streams, ArraysOfObjects, ArraysOfPrimitives, Iterables)
|
||||
}
|
||||
|
||||
templates add f("sortDescendingBy(order: (T) -> R)") {
|
||||
inline(true)
|
||||
|
||||
doc {
|
||||
"""
|
||||
Returns a list of all elements, sorted by results of specified *order* function.
|
||||
Returns a sorted list of all elements, in descending order by results of specified *order* function.
|
||||
"""
|
||||
}
|
||||
returns("List<T>")
|
||||
|
||||
@@ -92,13 +92,5 @@ fun snapshots(): List<GenericFunction> {
|
||||
body { "return toCollection(LinkedList<T>())" }
|
||||
}
|
||||
|
||||
templates add f("toSortedList()") {
|
||||
doc { "Returns a sorted list of all elements" }
|
||||
typeParam("T : Comparable<T>")
|
||||
returns("List<T>")
|
||||
body { "return toArrayList().sort()" }
|
||||
body(Iterables) { "return sort()" }
|
||||
}
|
||||
|
||||
return templates
|
||||
}
|
||||
Reference in New Issue
Block a user