Rename Stream<T> to Sequence<T> and provide migration path via deprecated types and functions.

This commit is contained in:
Ilya Ryzhenkov
2015-03-10 18:10:57 +03:00
parent 9684d217b3
commit e448f40756
47 changed files with 1906 additions and 556 deletions
@@ -97,6 +97,16 @@ public inline fun <K, V> Map<K, V>.all(predicate: (Map.Entry<K, V>) -> Boolean):
return true
}
/**
* Returns *true* if all elements match the given *predicate*
*/
public inline fun <T> Sequence<T>.all(predicate: (T) -> Boolean): Boolean {
for (element in this) if (!predicate(element)) return false
return true
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns *true* if all elements match the given *predicate*
*/
@@ -201,6 +211,16 @@ public fun <K, V> Map<K, V>.any(): Boolean {
return false
}
/**
* Returns *true* if collection has at least one element
*/
public fun <T> Sequence<T>.any(): Boolean {
for (element in this) return true
return false
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns *true* if collection has at least one element
*/
@@ -305,6 +325,16 @@ public inline fun <K, V> Map<K, V>.any(predicate: (Map.Entry<K, V>) -> Boolean):
return false
}
/**
* Returns *true* if any element matches the given [predicate]
*/
public inline fun <T> Sequence<T>.any(predicate: (T) -> Boolean): Boolean {
for (element in this) if (predicate(element)) return true
return false
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns *true* if any element matches the given [predicate]
*/
@@ -407,6 +437,17 @@ public fun <K, V> Map<K, V>.count(): Int {
return size()
}
/**
* Returns the number of elements
*/
public fun <T> Sequence<T>.count(): Int {
var count = 0
for (element in this) count++
return count
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns the number of elements
*/
@@ -522,6 +563,17 @@ public inline fun <K, V> Map<K, V>.count(predicate: (Map.Entry<K, V>) -> Boolean
return count
}
/**
* Returns the number of elements matching the given [predicate]
*/
public inline fun <T> Sequence<T>.count(predicate: (T) -> Boolean): Int {
var count = 0
for (element in this) if (predicate(element)) count++
return count
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns the number of elements matching the given [predicate]
*/
@@ -630,6 +682,17 @@ public inline fun <T, R> Iterable<T>.fold(initial: R, operation: (R, T) -> R): R
return accumulator
}
/**
* Accumulates value starting with *initial* value and applying *operation* from left to right to current accumulator value and each element
*/
public inline fun <T, R> Sequence<T>.fold(initial: R, operation: (R, T) -> R): R {
var accumulator = initial
for (element in this) accumulator = operation(accumulator, element)
return accumulator
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Accumulates value starting with *initial* value and applying *operation* from left to right to current accumulator value and each element
*/
@@ -857,6 +920,15 @@ public inline fun <K, V> Map<K, V>.forEach(operation: (Map.Entry<K, V>) -> Unit)
for (element in this) operation(element)
}
/**
* Performs the given *operation* on each element
*/
public inline fun <T> Sequence<T>.forEach(operation: (T) -> Unit): Unit {
for (element in this) operation(element)
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Performs the given *operation* on each element
*/
@@ -951,6 +1023,16 @@ public inline fun <T> Iterable<T>.forEachIndexed(operation: (Int, T) -> Unit): U
for (item in this) operation(index++, item)
}
/**
* Performs the given *operation* on each element, providing sequential index with the element
*/
public inline fun <T> Sequence<T>.forEachIndexed(operation: (Int, T) -> Unit): Unit {
var index = 0
for (item in this) operation(index++, item)
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Performs the given *operation* on each element, providing sequential index with the element
*/
@@ -1085,6 +1167,22 @@ public fun <T : Comparable<T>> Iterable<T>.max(): T? {
return max
}
/**
* Returns the largest element or null if there are no elements
*/
public fun <T : Comparable<T>> Sequence<T>.max(): T? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var max = iterator.next()
while (iterator.hasNext()) {
val e = iterator.next()
if (max < e) max = e
}
return max
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns the largest element or null if there are no elements
*/
@@ -1294,6 +1392,27 @@ public inline fun <R : Comparable<R>, T : Any> Iterable<T>.maxBy(f: (T) -> R): T
return maxElem
}
/**
* Returns the first element yielding the largest value of the given function or null if there are no elements
*/
public inline fun <R : Comparable<R>, T : Any> Sequence<T>.maxBy(f: (T) -> R): T? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var maxElem = iterator.next()
var maxValue = f(maxElem)
while (iterator.hasNext()) {
val e = iterator.next()
val v = f(e)
if (maxValue < v) {
maxElem = e
maxValue = v
}
}
return maxElem
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns the first element yielding the largest value of the given function or null if there are no elements
*/
@@ -1469,6 +1588,22 @@ public fun <T : Comparable<T>> Iterable<T>.min(): T? {
return min
}
/**
* Returns the smallest element or null if there are no elements
*/
public fun <T : Comparable<T>> Sequence<T>.min(): T? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var min = iterator.next()
while (iterator.hasNext()) {
val e = iterator.next()
if (min > e) min = e
}
return min
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns the smallest element or null if there are no elements
*/
@@ -1678,6 +1813,27 @@ public inline fun <R : Comparable<R>, T : Any> Iterable<T>.minBy(f: (T) -> R): T
return minElem
}
/**
* Returns the first element yielding the smallest value of the given function or null if there are no elements
*/
public inline fun <R : Comparable<R>, T : Any> Sequence<T>.minBy(f: (T) -> R): T? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var minElem = iterator.next()
var minValue = f(minElem)
while (iterator.hasNext()) {
val e = iterator.next()
val v = f(e)
if (minValue > v) {
minElem = e
minValue = v
}
}
return minElem
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns the first element yielding the smallest value of the given function or null if there are no elements
*/
@@ -1823,6 +1979,16 @@ public fun <K, V> Map<K, V>.none(): Boolean {
return true
}
/**
* Returns *true* if collection has no elements
*/
public fun <T> Sequence<T>.none(): Boolean {
for (element in this) return false
return true
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns *true* if collection has no elements
*/
@@ -1927,6 +2093,16 @@ public inline fun <K, V> Map<K, V>.none(predicate: (Map.Entry<K, V>) -> Boolean)
return true
}
/**
* Returns *true* if no elements match the given *predicate*
*/
public inline fun <T> Sequence<T>.none(predicate: (T) -> Boolean): Boolean {
for (element in this) if (predicate(element)) return false
return true
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns *true* if no elements match the given *predicate*
*/
@@ -2073,6 +2249,21 @@ public inline fun <T> Iterable<T>.reduce(operation: (T, T) -> T): T {
return accumulator
}
/**
* Accumulates value starting with the first element and applying *operation* from left to right to current accumulator value and each element
*/
public inline fun <T> Sequence<T>.reduce(operation: (T, T) -> T): T {
val iterator = this.iterator()
if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced")
var accumulator = iterator.next()
while (iterator.hasNext()) {
accumulator = operation(accumulator, iterator.next())
}
return accumulator
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Accumulates value starting with the first element and applying *operation* from left to right to current accumulator value and each element
*/
@@ -2352,6 +2543,19 @@ public inline fun <T> Iterable<T>.sumBy(transform: (T) -> Int): Int {
return sum
}
/**
* Returns the sum of all values produced by [transform] function from elements in the collection
*/
public inline fun <T> Sequence<T>.sumBy(transform: (T) -> Int): Int {
var sum: Int = 0
for (element in this) {
sum += transform(element)
}
return sum
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns the sum of all values produced by [transform] function from elements in the collection
*/
@@ -2484,6 +2688,19 @@ public inline fun <T> Iterable<T>.sumByDouble(transform: (T) -> Double): Double
return sum
}
/**
* Returns the sum of all values produced by [transform] function from elements in the collection
*/
public inline fun <T> Sequence<T>.sumByDouble(transform: (T) -> Double): Double {
var sum: Double = 0.0
for (element in this) {
sum += transform(element)
}
return sum
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns the sum of all values produced by [transform] function from elements in the collection
*/
+302
View File
@@ -431,6 +431,17 @@ public fun <T> Iterable<T>.contains(element: T): Boolean {
return indexOf(element) >= 0
}
/**
* Returns true if *element* is found in the collection
*/
public fun <T> Sequence<T>.contains(element: T): Boolean {
if (this is Collection<*>)
return contains(element)
return indexOf(element) >= 0
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns true if *element* is found in the collection
*/
@@ -526,6 +537,22 @@ public fun <T> List<T>.elementAt(index: Int): T {
return get(index)
}
/**
* Returns element at given *index*
*/
public fun <T> Sequence<T>.elementAt(index: Int): T {
val iterator = iterator()
var count = 0
while (iterator.hasNext()) {
val element = iterator.next()
if (index == count++)
return element
}
throw IndexOutOfBoundsException("Collection doesn't contain element at index")
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns element at given *index*
*/
@@ -668,6 +695,29 @@ public fun <T> List<T>.first(): T {
return this[0]
}
/**
* Returns first element.
* @throws NoSuchElementException if the collection is empty.
*/
public fun <T> Sequence<T>.first(): T {
when (this) {
is List<*> -> {
if (isEmpty())
throw NoSuchElementException("Collection is empty")
else
return this[0] as T
}
else -> {
val iterator = iterator()
if (!iterator.hasNext())
throw NoSuchElementException("Collection is empty")
return iterator.next()
}
}
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns first element.
* @throws NoSuchElementException if the collection is empty.
@@ -793,6 +843,17 @@ public inline fun <T> Iterable<T>.first(predicate: (T) -> Boolean): T {
* Returns the first element matching the given [predicate].
* @throws NoSuchElementException if no such element is found.
*/
public inline fun <T> Sequence<T>.first(predicate: (T) -> Boolean): T {
for (element in this) if (predicate(element)) return element
throw NoSuchElementException("No element matching predicate was found")
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* "Returns the first element matching the given [predicate].
* @throws NoSuchElementException if no such element is found.
*/
public inline fun <T> Stream<T>.first(predicate: (T) -> Boolean): T {
for (element in this) if (predicate(element)) return element
throw NoSuchElementException("No element matching predicate was found")
@@ -897,6 +958,28 @@ public fun <T> List<T>.firstOrNull(): T? {
return if (isEmpty()) null else this[0]
}
/**
* Returns the first element, or null if the collection is empty.
*/
public fun <T> Sequence<T>.firstOrNull(): T? {
when (this) {
is List<*> -> {
if (isEmpty())
return null
else
return this[0] as T
}
else -> {
val iterator = iterator()
if (!iterator.hasNext())
return null
return iterator.next()
}
}
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns the first element, or null if the collection is empty.
*/
@@ -1007,6 +1090,16 @@ public inline fun <T> Iterable<T>.firstOrNull(predicate: (T) -> Boolean): T? {
/**
* Returns the first element matching the given [predicate], or `null` if element was not found
*/
public inline fun <T> Sequence<T>.firstOrNull(predicate: (T) -> Boolean): T? {
for (element in this) if (predicate(element)) return element
return null
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns first element matching the given [predicate], or `null` if element was not found
*/
public inline fun <T> Stream<T>.firstOrNull(predicate: (T) -> Boolean): T? {
for (element in this) if (predicate(element)) return element
return null
@@ -1149,6 +1242,21 @@ public fun <T> Iterable<T>.indexOf(element: T): Int {
return -1
}
/**
* Returns first index of [element], or -1 if the collection does not contain element
*/
public fun <T> Sequence<T>.indexOf(element: T): Int {
var index = 0
for (item in this) {
if (element == item)
return index
index++
}
return -1
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns first index of [element], or -1 if the collection does not contain element
*/
@@ -1295,6 +1403,21 @@ public inline fun <T> List<T>.indexOfFirst(predicate: (T) -> Boolean): Int {
return -1
}
/**
* Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element
*/
public inline fun <T> Sequence<T>.indexOfFirst(predicate: (T) -> Boolean): Int {
var index = 0
for (item in this) {
if (predicate(item))
return index
index++
}
return -1
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element
*/
@@ -1454,6 +1577,22 @@ public inline fun <T> List<T>.indexOfLast(predicate: (T) -> Boolean): Int {
return -1
}
/**
* Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element
*/
public inline fun <T> Sequence<T>.indexOfLast(predicate: (T) -> Boolean): Int {
var lastIndex = -1
var index = 0
for (item in this) {
if (predicate(item))
lastIndex = index
index++
}
return lastIndex
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element
*/
@@ -1604,6 +1743,22 @@ public fun <T> List<T>.last(): T {
return this[lastIndex]
}
/**
* Returns the last element.
* @throws NoSuchElementException if the collection is empty.
*/
public fun <T> Sequence<T>.last(): T {
val iterator = iterator()
if (!iterator.hasNext())
throw NoSuchElementException("Collection is empty")
var last = iterator.next()
while (iterator.hasNext())
last = iterator.next()
return last
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns the last element.
* @throws NoSuchElementException if the collection is empty.
@@ -1798,6 +1953,25 @@ public inline fun <T> Iterable<T>.last(predicate: (T) -> Boolean): T {
return last as T
}
/**
* Returns the last element matching the given [predicate].
* @throws NoSuchElementException if no such element is found.
*/
public inline fun <T> Sequence<T>.last(predicate: (T) -> Boolean): T {
var last: T? = null
var found = false
for (element in this) {
if (predicate(element)) {
last = element
found = true
}
}
if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate")
return last as T
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns the last element matching the given [predicate].
* @throws NoSuchElementException if no such element is found.
@@ -1962,6 +2136,22 @@ public fun <T> Iterable<T>.lastIndexOf(element: T): Int {
return lastIndex
}
/**
* Returns last index of *element*, or -1 if the collection does not contain element
*/
public fun <T> Sequence<T>.lastIndexOf(element: T): Int {
var lastIndex = -1
var index = 0
for (item in this) {
if (element == item)
lastIndex = index
index++
}
return lastIndex
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns last index of *element*, or -1 if the collection does not contain element
*/
@@ -2064,6 +2254,21 @@ public fun <T> List<T>.lastOrNull(): T? {
return if (isEmpty()) null else this[size() - 1]
}
/**
* Returns the last element, or `null` if the collection is empty
*/
public fun <T> Sequence<T>.lastOrNull(): T? {
val iterator = iterator()
if (!iterator.hasNext())
return null
var last = iterator.next()
while (iterator.hasNext())
last = iterator.next()
return last
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns the last element, or `null` if the collection is empty
*/
@@ -2214,6 +2419,21 @@ public inline fun <T> Iterable<T>.lastOrNull(predicate: (T) -> Boolean): T? {
return last
}
/**
* Returns the last element matching the given [predicate], or `null` if no such element was found.
*/
public inline fun <T> Sequence<T>.lastOrNull(predicate: (T) -> Boolean): T? {
var last: T? = null
for (element in this) {
if (predicate(element)) {
last = element
}
}
return last
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns the last element matching the given [predicate], or `null` if no such element was found.
*/
@@ -2372,6 +2592,30 @@ public fun <T> List<T>.single(): T {
}
}
/**
* Returns the single element, or throws an exception if the collection is empty or has more than one element.
*/
public fun <T> Sequence<T>.single(): T {
when (this) {
is List<*> -> return when (size()) {
0 -> throw NoSuchElementException("Collection is empty")
1 -> this[0] as T
else -> throw IllegalArgumentException("Collection has more than one element")
}
else -> {
val iterator = iterator()
if (!iterator.hasNext())
throw NoSuchElementException("Collection is empty")
var single = iterator.next()
if (iterator.hasNext())
throw IllegalArgumentException("Collection has more than one element")
return single
}
}
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns the single element, or throws an exception if the collection is empty or has more than one element.
*/
@@ -2575,6 +2819,25 @@ public inline fun <T> Iterable<T>.single(predicate: (T) -> Boolean): T {
return single as T
}
/**
* Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element
*/
public inline fun <T> Sequence<T>.single(predicate: (T) -> Boolean): T {
var single: T? = null
var found = false
for (element in this) {
if (predicate(element)) {
if (found) throw IllegalArgumentException("Collection contains more than one matching element")
single = element
found = true
}
}
if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate")
return single as T
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element
*/
@@ -2697,6 +2960,26 @@ public fun <T> List<T>.singleOrNull(): T? {
return if (size() == 1) this[0] else null
}
/**
* Returns single element, or `null` if the collection is empty or has more than one element.
*/
public fun <T> Sequence<T>.singleOrNull(): T? {
when (this) {
is List<*> -> return if (size() == 1) this[0] as T else null
else -> {
val iterator = iterator()
if (!iterator.hasNext())
return null
var single = iterator.next()
if (iterator.hasNext())
return null
return single
}
}
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns single element, or `null` if the collection is empty or has more than one element.
*/
@@ -2892,6 +3175,25 @@ public inline fun <T> Iterable<T>.singleOrNull(predicate: (T) -> Boolean): T? {
return single
}
/**
* Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found
*/
public inline fun <T> Sequence<T>.singleOrNull(predicate: (T) -> Boolean): T? {
var single: T? = null
var found = false
for (element in this) {
if (predicate(element)) {
if (found) return null
single = element
found = true
}
}
if (!found) return null
return single
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found
*/
@@ -161,6 +161,15 @@ public fun <T> Iterable<T>.drop(n: Int): List<T> {
return list
}
/**
* Returns a sequence containing all elements except first [n] elements
*/
public fun <T> Sequence<T>.drop(n: Int): Sequence<T> {
return DropSequence(this, n)
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns a stream containing all elements except first [n] elements
*/
@@ -335,6 +344,15 @@ public inline fun <T> Iterable<T>.dropWhile(predicate: (T) -> Boolean): List<T>
return list
}
/**
* Returns a sequence containing all elements except first elements that satisfy the given [predicate]
*/
public fun <T> Sequence<T>.dropWhile(predicate: (T) -> Boolean): Sequence<T> {
return DropWhileSequence(this, predicate)
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns a stream containing all elements except first elements that satisfy the given [predicate]
*/
@@ -423,6 +441,15 @@ public inline fun <T> Iterable<T>.filter(predicate: (T) -> Boolean): List<T> {
return filterTo(ArrayList<T>(), predicate)
}
/**
* Returns a sequence containing all elements matching the given [predicate]
*/
public fun <T> Sequence<T>.filter(predicate: (T) -> Boolean): Sequence<T> {
return FilteringSequence(this, true, predicate)
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns a stream containing all elements matching the given [predicate]
*/
@@ -507,6 +534,15 @@ public inline fun <T> Iterable<T>.filterNot(predicate: (T) -> Boolean): List<T>
return filterNotTo(ArrayList<T>(), predicate)
}
/**
* Returns a sequence containing all elements not matching the given [predicate]
*/
public fun <T> Sequence<T>.filterNot(predicate: (T) -> Boolean): Sequence<T> {
return FilteringSequence(this, false, predicate)
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns a stream containing all elements not matching the given [predicate]
*/
@@ -535,6 +571,15 @@ public fun <T : Any> Iterable<T?>.filterNotNull(): List<T> {
return filterNotNullTo(ArrayList<T>())
}
/**
* Returns a sequence containing all elements that are not null
*/
public fun <T : Any> Sequence<T?>.filterNotNull(): Sequence<T> {
return FilteringSequence(this, false, { it == null }) as Sequence<T>
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns a stream containing all elements that are not null
*/
@@ -558,6 +603,16 @@ public fun <C : MutableCollection<in T>, T : Any> Iterable<T?>.filterNotNullTo(d
return destination
}
/**
* Appends all elements that are not null to the given [destination]
*/
public fun <C : MutableCollection<in T>, T : Any> Sequence<T?>.filterNotNullTo(destination: C): C {
for (element in this) if (element != null) destination.add(element)
return destination
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Appends all elements that are not null to the given [destination]
*/
@@ -646,6 +701,16 @@ public inline fun <T, C : MutableCollection<in T>> Iterable<T>.filterNotTo(desti
return destination
}
/**
* Appends all elements not matching the given [predicate] to the given [destination]
*/
public inline fun <T, C : MutableCollection<in T>> Sequence<T>.filterNotTo(destination: C, predicate: (T) -> Boolean): C {
for (element in this) if (!predicate(element)) destination.add(element)
return destination
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Appends all elements not matching the given [predicate] to the given [destination]
*/
@@ -742,6 +807,16 @@ public inline fun <T, C : MutableCollection<in T>> Iterable<T>.filterTo(destinat
return destination
}
/**
* Appends all elements matching the given [predicate] into the given [destination]
*/
public inline fun <T, C : MutableCollection<in T>> Sequence<T>.filterTo(destination: C, predicate: (T) -> Boolean): C {
for (element in this) if (predicate(element)) destination.add(element)
return destination
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Appends all elements matching the given [predicate] into the given [destination]
*/
@@ -1046,6 +1121,15 @@ public fun <T> Iterable<T>.take(n: Int): List<T> {
return list
}
/**
* Returns a sequence containing first *n* elements
*/
public fun <T> Sequence<T>.take(n: Int): Sequence<T> {
return TakeSequence(this, n)
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns a stream containing first *n* elements
*/
@@ -1190,6 +1274,15 @@ public inline fun <T> Iterable<T>.takeWhile(predicate: (T) -> Boolean): List<T>
return list
}
/**
* Returns a sequence containing first elements satisfying the given [predicate]
*/
public fun <T> Sequence<T>.takeWhile(predicate: (T) -> Boolean): Sequence<T> {
return TakeWhileSequence(this, predicate)
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns a stream containing first elements satisfying the given [predicate]
*/
+71 -4
View File
@@ -270,7 +270,16 @@ public inline fun <T, R, V> Iterable<T>.merge(other: Iterable<R>, transform: (T,
}
/**
* Returns a stream of values built from elements of both collections with same indexes using provided *transform*. Stream has length of shortest stream.
* Returns a sequence of values built from elements of both collections with same indexes using provided *transform*. Resulting sequence has length of shortest input sequences.
*/
public fun <T, R, V> Sequence<T>.merge(sequence: Sequence<R>, transform: (T, R) -> V): Sequence<V> {
return MergingSequence(this, sequence, transform)
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns a stream of values built from elements of both collections with same indexes using provided *transform*. Resulting stream has length of shortest input streams.
*/
public fun <T, R, V> Stream<T>.merge(stream: Stream<R>, transform: (T, R) -> V): Stream<V> {
return MergingStream(this, stream, transform)
@@ -456,6 +465,26 @@ public inline fun <T> Iterable<T>.partition(predicate: (T) -> Boolean): Pair<Lis
return Pair(first, second)
}
/**
* Splits original collection into pair of collections,
* where *first* collection contains elements for which predicate yielded *true*,
* while *second* collection contains elements for which predicate yielded *false*
*/
public inline fun <T> Sequence<T>.partition(predicate: (T) -> Boolean): Pair<List<T>, List<T>> {
val first = ArrayList<T>()
val second = ArrayList<T>()
for (element in this) {
if (predicate(element)) {
first.add(element)
} else {
second.add(element)
}
}
return Pair(first, second)
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Splits original collection into pair of collections,
* where *first* collection contains elements for which predicate yielded *true*,
@@ -673,7 +702,16 @@ public fun <T> Iterable<T>.plus(collection: Iterable<T>): List<T> {
}
/**
* Returns a stream containing all elements of original stream and then all elements of the given *collection*
* Returns a sequence containing all elements of original sequence and then all elements of the given [collection]
*/
public fun <T> Sequence<T>.plus(collection: Iterable<T>): Sequence<T> {
return MultiSequence(sequenceOf(this, collection.sequence()))
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns a stream containing all elements of original stream and then all elements of the given [collection]
*/
public fun <T> Stream<T>.plus(collection: Iterable<T>): Stream<T> {
return Multistream(streamOf(this, collection.stream()))
@@ -769,6 +807,15 @@ public fun <T> Iterable<T>.plus(element: T): List<T> {
return answer
}
/**
* Returns a sequence containing all elements of original sequence and then the given element
*/
public fun <T> Sequence<T>.plus(element: T): Sequence<T> {
return MultiSequence(sequenceOf(this, sequenceOf(element)))
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns a stream containing all elements of original stream and then the given element
*/
@@ -777,7 +824,16 @@ public fun <T> Stream<T>.plus(element: T): Stream<T> {
}
/**
* Returns a stream containing all elements of original stream and then all elements of the given *stream*
* Returns a sequence containing all elements of original sequence and then all elements of the given [sequence]
*/
public fun <T> Sequence<T>.plus(sequence: Sequence<T>): Sequence<T> {
return MultiSequence(sequenceOf(this, sequence))
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns a stream containing all elements of original stream and then all elements of the given [stream]
*/
public fun <T> Stream<T>.plus(stream: Stream<T>): Stream<T> {
return Multistream(streamOf(this, stream))
@@ -937,7 +993,18 @@ public fun String.zip(other: String): List<Pair<Char, Char>> {
}
/**
* Returns a stream of pairs built from elements of both collections with same indexes. Stream has length of shortest stream.
* Returns a sequence of pairs built from elements of both collections with same indexes.
* Resulting sequence has length of shortest input sequences.
*/
public fun <T, R> Sequence<T>.zip(sequence: Sequence<R>): Sequence<Pair<T, R>> {
return MergingSequence(this, sequence) { (t1, t2) -> t1 to t2 }
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns a stream of pairs built from elements of both collections with same indexes.
* Resulting stream has length of shortest input streams.
*/
public fun <T, R> Stream<T>.zip(stream: Stream<R>): Stream<Pair<T, R>> {
return MergingStream(this, stream) { (t1, t2) -> t1 to t2 }
+14
View File
@@ -45,6 +45,20 @@ public fun <T : Any> List<T?>.requireNoNulls(): List<T> {
return this as List<T>
}
/**
* Returns an original collection containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements
*/
public fun <T : Any> Sequence<T?>.requireNoNulls(): Sequence<T> {
return FilteringSequence(this) {
if (it == null) {
throw IllegalArgumentException("null element found in $this")
}
true
} as Sequence<T>
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns an original collection containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements
*/
+133 -1
View File
@@ -94,7 +94,16 @@ public inline fun <R> String.flatMap(transform: (Char) -> Iterable<R>): List<R>
}
/**
* Returns a single stream of all elements streamed from results of *transform* function being invoked on each element of original stream
* Returns a single sequence of all elements from results of *transform* function being invoked on each element of original sequence
*/
public fun <T, R> Sequence<T>.flatMap(transform: (T) -> Sequence<R>): Sequence<R> {
return FlatteningSequence(this, transform)
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns a single stream of all elements from results of *transform* function being invoked on each element of original stream
*/
public fun <T, R> Stream<T>.flatMap(transform: (T) -> Stream<R>): Stream<R> {
return FlatteningStream(this, transform)
@@ -232,6 +241,19 @@ public inline fun <R, C : MutableCollection<in R>> String.flatMapTo(destination:
return destination
}
/**
* Appends all elements yielded from results of *transform* function being invoked on each element of original sequence, to the given *destination*
*/
public inline fun <T, R, C : MutableCollection<in R>> Sequence<T>.flatMapTo(destination: C, transform: (T) -> Sequence<R>): C {
for (element in this) {
val list = transform(element)
destination.addAll(list)
}
return destination
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Appends all elements yielded from results of *transform* function being invoked on each element of original stream, to the given *destination*
*/
@@ -313,6 +335,15 @@ public inline fun <T, K> Iterable<T>.groupBy(toKey: (T) -> K): Map<K, List<T>> {
return groupByTo(LinkedHashMap<K, MutableList<T>>(), toKey)
}
/**
* Returns a map of the elements in original collection grouped by the result of given *toKey* function
*/
public inline fun <T, K> Sequence<T>.groupBy(toKey: (T) -> K): Map<K, List<T>> {
return groupByTo(LinkedHashMap<K, MutableList<T>>(), toKey)
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns a map of the elements in original collection grouped by the result of given *toKey* function
*/
@@ -447,6 +478,20 @@ public inline fun <T, K> Iterable<T>.groupByTo(map: MutableMap<K, MutableList<T>
return map
}
/**
* Appends elements from original collection grouped by the result of given *toKey* function to the given *map*
*/
public inline fun <T, K> Sequence<T>.groupByTo(map: MutableMap<K, MutableList<T>>, toKey: (T) -> K): Map<K, MutableList<T>> {
for (element in this) {
val key = toKey(element)
val list = map.getOrPut(key) { ArrayList<T>() }
list.add(element)
}
return map
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Appends elements from original collection grouped by the result of given *toKey* function to the given *map*
*/
@@ -548,6 +593,15 @@ public inline fun <K, V, R> Map<K, V>.map(transform: (Map.Entry<K, V>) -> R): Li
return mapTo(ArrayList<R>(), transform)
}
/**
* Returns a sequence containing the results of applying the given *transform* function to each element of the original sequence
*/
public fun <T, R> Sequence<T>.map(transform: (T) -> R): Sequence<R> {
return TransformingSequence(this, transform)
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns a stream containing the results of applying the given *transform* function to each element of the original stream
*/
@@ -632,6 +686,15 @@ public inline fun <T, R> Iterable<T>.mapIndexed(transform: (Int, T) -> R): List<
return mapIndexedTo(ArrayList<R>(collectionSizeOrDefault(10)), transform)
}
/**
* Returns a sequence containing the results of applying the given *transform* function to each element and its index of the original sequence
*/
public fun <T, R> Sequence<T>.mapIndexed(transform: (Int, T) -> R): Sequence<R> {
return TransformingIndexedSequence(this, transform)
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns a stream containing the results of applying the given *transform* function to each element and its index of the original stream
*/
@@ -767,6 +830,19 @@ public inline fun <K, V, R, C : MutableCollection<in R>> Map<K, V>.mapIndexedTo(
return destination
}
/**
* Appends transformed elements and their indices of the original collection using the given *transform* function
* to the given *destination*
*/
public inline fun <T, R, C : MutableCollection<in R>> Sequence<T>.mapIndexedTo(destination: C, transform: (Int, T) -> R): C {
var index = 0
for (item in this)
destination.add(transform(index++, item))
return destination
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Appends transformed elements and their indices of the original collection using the given *transform* function
* to the given *destination*
@@ -803,6 +879,15 @@ public inline fun <T : Any, R> Iterable<T?>.mapNotNull(transform: (T) -> R): Lis
return mapNotNullTo(ArrayList<R>(), transform)
}
/**
* Returns a sequence containing the results of applying the given *transform* function to each non-null element of the original sequence
*/
public fun <T : Any, R> Sequence<T?>.mapNotNull(transform: (T) -> R): Sequence<R> {
return TransformingSequence(FilteringSequence(this, false, { it == null }) as Sequence<T>, transform)
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns a stream containing the results of applying the given *transform* function to each non-null element of the original stream
*/
@@ -836,6 +921,21 @@ public inline fun <T : Any, R, C : MutableCollection<in R>> Iterable<T?>.mapNotN
return destination
}
/**
* Appends transformed non-null elements of original collection using the given *transform* function
* to the given *destination*
*/
public inline fun <T : Any, R, C : MutableCollection<in R>> Sequence<T?>.mapNotNullTo(destination: C, transform: (T) -> R): C {
for (element in this) {
if (element != null) {
destination.add(transform(element))
}
}
return destination
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Appends transformed non-null elements of original collection using the given *transform* function
* to the given *destination*
@@ -959,6 +1059,18 @@ public inline fun <K, V, R, C : MutableCollection<in R>> Map<K, V>.mapTo(destina
return destination
}
/**
* Appends transformed elements of the original collection using the given *transform* function
* to the given *destination*
*/
public inline fun <T, R, C : MutableCollection<in R>> Sequence<T>.mapTo(destination: C, transform: (T) -> R): C {
for (item in this)
destination.add(transform(item))
return destination
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Appends transformed elements of the original collection using the given *transform* function
* to the given *destination*
@@ -1049,6 +1161,15 @@ public fun <T> Iterable<T>.withIndex(): Iterable<IndexedValue<T>> {
return IndexingIterable { iterator() }
}
/**
* Returns a sequence of [IndexedValue] for each element of the original sequence
*/
public fun <T> Sequence<T>.withIndex(): Sequence<IndexedValue<T>> {
return IndexingSequence(this)
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns a stream of [IndexedValue] for each element of the original stream
*/
@@ -1153,6 +1274,17 @@ public fun <T> Iterable<T>.withIndices(): List<Pair<Int, T>> {
return mapTo(ArrayList<Pair<Int, T>>(), { index++ to it })
}
/**
* Returns a sequence containing pairs of each element of the original collection and their index
*/
deprecated("Use withIndex() instead.")
public fun <T> Sequence<T>.withIndices(): Sequence<Pair<Int, T>> {
var index = 0
return TransformingSequence(this, { index++ to it })
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns a stream containing pairs of each element of the original collection and their index
*/
@@ -21,6 +21,20 @@ public fun Iterable<Int>.sum(): Int {
return sum
}
/**
* Returns the sum of all elements in the collection
*/
public fun Sequence<Int>.sum(): Int {
val iterator = iterator()
var sum: Int = 0
while (iterator.hasNext()) {
sum += iterator.next()
}
return sum
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns the sum of all elements in the collection
*/
@@ -45,6 +59,20 @@ public fun Iterable<Long>.sum(): Long {
return sum
}
/**
* Returns the sum of all elements in the collection
*/
public fun Sequence<Long>.sum(): Long {
val iterator = iterator()
var sum: Long = 0
while (iterator.hasNext()) {
sum += iterator.next()
}
return sum
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns the sum of all elements in the collection
*/
@@ -69,6 +97,20 @@ public fun Iterable<Double>.sum(): Double {
return sum
}
/**
* Returns the sum of all elements in the collection
*/
public fun Sequence<Double>.sum(): Double {
val iterator = iterator()
var sum: Double = 0.0
while (iterator.hasNext()) {
sum += iterator.next()
}
return sum
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns the sum of all elements in the collection
*/
@@ -93,6 +135,20 @@ public fun Iterable<Float>.sum(): Float {
return sum
}
/**
* Returns the sum of all elements in the collection
*/
public fun Sequence<Float>.sum(): Float {
val iterator = iterator()
var sum: Float = 0.0f
while (iterator.hasNext()) {
sum += iterator.next()
}
return sum
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns the sum of all elements in the collection
*/
@@ -272,6 +272,17 @@ public fun <T : Comparable<T>> Iterable<T>.toSortedList(): List<T> {
return sortedList
}
/**
* Returns a sorted list of all elements
*/
public fun <T : Comparable<T>> Sequence<T>.toSortedList(): List<T> {
val sortedList = toArrayList()
java.util.Collections.sort(sortedList)
return sortedList
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns a sorted list of all elements
*/
@@ -381,6 +392,18 @@ public fun <T, V : Comparable<V>> Iterable<T>.toSortedListBy(order: (T) -> V): L
return sortedList
}
/**
* Returns a sorted list of all elements, ordered by results of specified *order* function.
*/
public fun <T, V : Comparable<V>> Sequence<T>.toSortedListBy(order: (T) -> V): List<T> {
val sortedList = toArrayList()
val sortBy: Comparator<T> = compareBy(order)
java.util.Collections.sort(sortedList, sortBy)
return sortedList
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns a sorted list of all elements, ordered by results of specified *order* function.
*/
@@ -0,0 +1,315 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import java.util.*
import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js
/**
* Returns a sequence from the given collection
*/
public fun <T> Array<out T>.sequence(): Sequence<T> {
return object : Sequence<T> {
override fun iterator(): Iterator<T> {
return this@sequence.iterator()
}
}
}
/**
* Returns a sequence from the given collection
*/
public fun BooleanArray.sequence(): Sequence<Boolean> {
return object : Sequence<Boolean> {
override fun iterator(): Iterator<Boolean> {
return this@sequence.iterator()
}
}
}
/**
* Returns a sequence from the given collection
*/
public fun ByteArray.sequence(): Sequence<Byte> {
return object : Sequence<Byte> {
override fun iterator(): Iterator<Byte> {
return this@sequence.iterator()
}
}
}
/**
* Returns a sequence from the given collection
*/
public fun CharArray.sequence(): Sequence<Char> {
return object : Sequence<Char> {
override fun iterator(): Iterator<Char> {
return this@sequence.iterator()
}
}
}
/**
* Returns a sequence from the given collection
*/
public fun DoubleArray.sequence(): Sequence<Double> {
return object : Sequence<Double> {
override fun iterator(): Iterator<Double> {
return this@sequence.iterator()
}
}
}
/**
* Returns a sequence from the given collection
*/
public fun FloatArray.sequence(): Sequence<Float> {
return object : Sequence<Float> {
override fun iterator(): Iterator<Float> {
return this@sequence.iterator()
}
}
}
/**
* Returns a sequence from the given collection
*/
public fun IntArray.sequence(): Sequence<Int> {
return object : Sequence<Int> {
override fun iterator(): Iterator<Int> {
return this@sequence.iterator()
}
}
}
/**
* Returns a sequence from the given collection
*/
public fun LongArray.sequence(): Sequence<Long> {
return object : Sequence<Long> {
override fun iterator(): Iterator<Long> {
return this@sequence.iterator()
}
}
}
/**
* Returns a sequence from the given collection
*/
public fun ShortArray.sequence(): Sequence<Short> {
return object : Sequence<Short> {
override fun iterator(): Iterator<Short> {
return this@sequence.iterator()
}
}
}
/**
* Returns a sequence from the given collection
*/
public fun <T> Iterable<T>.sequence(): Sequence<T> {
return object : Sequence<T> {
override fun iterator(): Iterator<T> {
return this@sequence.iterator()
}
}
}
/**
* Returns a sequence from the given collection
*/
public fun <K, V> Map<K, V>.sequence(): Sequence<Map.Entry<K, V>> {
return object : Sequence<Map.Entry<K, V>> {
override fun iterator(): Iterator<Map.Entry<K, V>> {
return this@sequence.iterator()
}
}
}
/**
* Returns a sequence from the given collection
*/
public fun <T> Sequence<T>.sequence(): Sequence<T> {
return this
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns a stream from the given collection
*/
public fun <T> Stream<T>.stream(): Stream<T> {
return this
}
/**
* Returns a sequence from the given collection
*/
public fun String.sequence(): Sequence<Char> {
return object : Sequence<Char> {
override fun iterator(): Iterator<Char> {
return this@sequence.iterator()
}
}
}
/**
* Returns a sequence from the given collection
*/
deprecated("Use sequence() instead")
public fun <T> Array<out T>.stream(): Stream<T> {
val sequence = sequence()
return object : Stream<T> {
override fun iterator(): Iterator<T> {
return sequence.iterator()
}
}
}
/**
* Returns a sequence from the given collection
*/
deprecated("Use sequence() instead")
public fun BooleanArray.stream(): Stream<Boolean> {
val sequence = sequence()
return object : Stream<Boolean> {
override fun iterator(): Iterator<Boolean> {
return sequence.iterator()
}
}
}
/**
* Returns a sequence from the given collection
*/
deprecated("Use sequence() instead")
public fun ByteArray.stream(): Stream<Byte> {
val sequence = sequence()
return object : Stream<Byte> {
override fun iterator(): Iterator<Byte> {
return sequence.iterator()
}
}
}
/**
* Returns a sequence from the given collection
*/
deprecated("Use sequence() instead")
public fun CharArray.stream(): Stream<Char> {
val sequence = sequence()
return object : Stream<Char> {
override fun iterator(): Iterator<Char> {
return sequence.iterator()
}
}
}
/**
* Returns a sequence from the given collection
*/
deprecated("Use sequence() instead")
public fun DoubleArray.stream(): Stream<Double> {
val sequence = sequence()
return object : Stream<Double> {
override fun iterator(): Iterator<Double> {
return sequence.iterator()
}
}
}
/**
* Returns a sequence from the given collection
*/
deprecated("Use sequence() instead")
public fun FloatArray.stream(): Stream<Float> {
val sequence = sequence()
return object : Stream<Float> {
override fun iterator(): Iterator<Float> {
return sequence.iterator()
}
}
}
/**
* Returns a sequence from the given collection
*/
deprecated("Use sequence() instead")
public fun IntArray.stream(): Stream<Int> {
val sequence = sequence()
return object : Stream<Int> {
override fun iterator(): Iterator<Int> {
return sequence.iterator()
}
}
}
/**
* Returns a sequence from the given collection
*/
deprecated("Use sequence() instead")
public fun LongArray.stream(): Stream<Long> {
val sequence = sequence()
return object : Stream<Long> {
override fun iterator(): Iterator<Long> {
return sequence.iterator()
}
}
}
/**
* Returns a sequence from the given collection
*/
deprecated("Use sequence() instead")
public fun ShortArray.stream(): Stream<Short> {
val sequence = sequence()
return object : Stream<Short> {
override fun iterator(): Iterator<Short> {
return sequence.iterator()
}
}
}
/**
* Returns a sequence from the given collection
*/
deprecated("Use sequence() instead")
public fun <T> Iterable<T>.stream(): Stream<T> {
val sequence = sequence()
return object : Stream<T> {
override fun iterator(): Iterator<T> {
return sequence.iterator()
}
}
}
/**
* Returns a sequence from the given collection
*/
deprecated("Use sequence() instead")
public fun <K, V> Map<K, V>.stream(): Stream<Map.Entry<K, V>> {
val sequence = sequence()
return object : Stream<Map.Entry<K, V>> {
override fun iterator(): Iterator<Map.Entry<K, V>> {
return sequence.iterator()
}
}
}
/**
* Returns a sequence from the given collection
*/
deprecated("Use sequence() instead")
public fun String.stream(): Stream<Char> {
val sequence = sequence()
return object : Stream<Char> {
override fun iterator(): Iterator<Char> {
return sequence.iterator()
}
}
}
@@ -97,6 +97,15 @@ public fun <T> Iterable<T>.toArrayList(): ArrayList<T> {
return toCollection(ArrayList<T>(collectionSizeOrDefault(10)))
}
/**
* Returns an ArrayList of all elements
*/
public fun <T> Sequence<T>.toArrayList(): ArrayList<T> {
return toCollection(ArrayList<T>())
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns an ArrayList of all elements
*/
@@ -211,6 +220,18 @@ public fun <T, C : MutableCollection<in T>> Iterable<T>.toCollection(collection:
return collection
}
/**
* Appends all elements to the given *collection*
*/
public fun <T, C : MutableCollection<in T>> Sequence<T>.toCollection(collection: C): C {
for (item in this) {
collection.add(item)
}
return collection
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Appends all elements to the given *collection*
*/
@@ -301,6 +322,15 @@ public fun <T> Iterable<T>.toHashSet(): HashSet<T> {
return toCollection(HashSet<T>())
}
/**
* Returns a HashSet of all elements
*/
public fun <T> Sequence<T>.toHashSet(): HashSet<T> {
return toCollection(HashSet<T>())
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns a HashSet of all elements
*/
@@ -385,6 +415,15 @@ public fun <T> Iterable<T>.toLinkedList(): LinkedList<T> {
return toCollection(LinkedList<T>())
}
/**
* Returns a LinkedList containing all elements
*/
public fun <T> Sequence<T>.toLinkedList(): LinkedList<T> {
return toCollection(LinkedList<T>())
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns a LinkedList containing all elements
*/
@@ -497,6 +536,15 @@ public fun <T> Iterable<T>.toList(): List<T> {
return toCollection(ArrayList<T>(collectionSizeOrDefault(10)))
}
/**
* Returns a List containing all elements
*/
public fun <T> Sequence<T>.toList(): List<T> {
return toCollection(ArrayList<T>())
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns a List containing all elements
*/
@@ -621,6 +669,19 @@ public inline fun <T, K> Iterable<T>.toMap(selector: (T) -> K): Map<K, T> {
return result
}
/**
* Returns Map containing all the values from the given collection indexed by *selector*
*/
public inline fun <T, K> Sequence<T>.toMap(selector: (T) -> K): Map<K, T> {
val result = LinkedHashMap<K, T>()
for (element in this) {
result.put(selector(element), element)
}
return result
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns Map containing all the values from the given collection indexed by *selector*
*/
@@ -713,6 +774,15 @@ public fun <T> Iterable<T>.toSet(): Set<T> {
return toCollection(LinkedHashSet<T>())
}
/**
* Returns a Set of all elements
*/
public fun <T> Sequence<T>.toSet(): Set<T> {
return toCollection(LinkedHashSet<T>())
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns a Set of all elements
*/
@@ -797,6 +867,15 @@ public fun <T> Iterable<T>.toSortedSet(): SortedSet<T> {
return toCollection(TreeSet<T>())
}
/**
* Returns a SortedSet of all elements
*/
public fun <T> Sequence<T>.toSortedSet(): SortedSet<T> {
return toCollection(TreeSet<T>())
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns a SortedSet of all elements
*/
@@ -467,6 +467,15 @@ public inline fun <reified R> Iterable<*>.filterIsInstance(): List<R> {
return filterIsInstanceTo(ArrayList<R>())
}
/**
* Returns a sequence containing all elements that are instances of specified type parameter R
*/
public inline fun <reified R> Sequence<*>.filterIsInstance(): Sequence<R> {
return FilteringSequence(this, true, { it is R }) as Sequence<R>
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns a stream containing all elements that are instances of specified type parameter R
*/
@@ -488,6 +497,15 @@ public fun <R> Iterable<*>.filterIsInstance(klass: Class<R>): List<R> {
return filterIsInstanceTo(ArrayList<R>(), klass)
}
/**
* Returns a sequence containing all elements that are instances of specified class
*/
public fun <R> Sequence<*>.filterIsInstance(klass: Class<R>): Sequence<R> {
return FilteringSequence(this, true, { klass.isInstance(it) }) as Sequence<R>
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns a stream containing all elements that are instances of specified class
*/
@@ -511,6 +529,16 @@ public inline fun <reified R, C : MutableCollection<in R>> Iterable<*>.filterIsI
return destination
}
/**
* Appends all elements that are instances of specified type parameter R to the given *destination*
*/
public inline fun <reified R, C : MutableCollection<in R>> Sequence<*>.filterIsInstanceTo(destination: C): C {
for (element in this) if (element is R) destination.add(element)
return destination
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Appends all elements that are instances of specified type parameter R to the given *destination*
*/
@@ -535,6 +563,16 @@ public fun <C : MutableCollection<in R>, R> Iterable<*>.filterIsInstanceTo(desti
return destination
}
/**
* Appends all elements that are instances of specified class to the given *destination*
*/
public fun <C : MutableCollection<in R>, R> Sequence<*>.filterIsInstanceTo(destination: C, klass: Class<R>): C {
for (element in this) if (klass.isInstance(element)) destination.add(element as R)
return destination
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Appends all elements that are instances of specified class to the given *destination*
*/
-150
View File
@@ -1,150 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import java.util.*
import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js
/**
* Returns a stream from the given collection
*/
public fun <T> Array<out T>.stream(): Stream<T> {
return object : Stream<T> {
override fun iterator(): Iterator<T> {
return this@stream.iterator()
}
}
}
/**
* Returns a stream from the given collection
*/
public fun BooleanArray.stream(): Stream<Boolean> {
return object : Stream<Boolean> {
override fun iterator(): Iterator<Boolean> {
return this@stream.iterator()
}
}
}
/**
* Returns a stream from the given collection
*/
public fun ByteArray.stream(): Stream<Byte> {
return object : Stream<Byte> {
override fun iterator(): Iterator<Byte> {
return this@stream.iterator()
}
}
}
/**
* Returns a stream from the given collection
*/
public fun CharArray.stream(): Stream<Char> {
return object : Stream<Char> {
override fun iterator(): Iterator<Char> {
return this@stream.iterator()
}
}
}
/**
* Returns a stream from the given collection
*/
public fun DoubleArray.stream(): Stream<Double> {
return object : Stream<Double> {
override fun iterator(): Iterator<Double> {
return this@stream.iterator()
}
}
}
/**
* Returns a stream from the given collection
*/
public fun FloatArray.stream(): Stream<Float> {
return object : Stream<Float> {
override fun iterator(): Iterator<Float> {
return this@stream.iterator()
}
}
}
/**
* Returns a stream from the given collection
*/
public fun IntArray.stream(): Stream<Int> {
return object : Stream<Int> {
override fun iterator(): Iterator<Int> {
return this@stream.iterator()
}
}
}
/**
* Returns a stream from the given collection
*/
public fun LongArray.stream(): Stream<Long> {
return object : Stream<Long> {
override fun iterator(): Iterator<Long> {
return this@stream.iterator()
}
}
}
/**
* Returns a stream from the given collection
*/
public fun ShortArray.stream(): Stream<Short> {
return object : Stream<Short> {
override fun iterator(): Iterator<Short> {
return this@stream.iterator()
}
}
}
/**
* Returns a stream from the given collection
*/
public fun <T> Iterable<T>.stream(): Stream<T> {
return object : Stream<T> {
override fun iterator(): Iterator<T> {
return this@stream.iterator()
}
}
}
/**
* Returns a stream from the given collection
*/
public fun <K, V> Map<K, V>.stream(): Stream<Map.Entry<K, V>> {
return object : Stream<Map.Entry<K, V>> {
override fun iterator(): Iterator<Map.Entry<K, V>> {
return this@stream.iterator()
}
}
}
/**
* Returns a stream from the given collection
*/
public fun <T> Stream<T>.stream(): Stream<T> {
return this
}
/**
* Returns a stream from the given collection
*/
public fun String.stream(): Stream<Char> {
return object : Stream<Char> {
override fun iterator(): Iterator<Char> {
return this@stream.iterator()
}
}
}
@@ -209,6 +209,28 @@ public fun <T, A : Appendable> Iterable<T>.joinTo(buffer: A, separator: String =
return buffer
}
/**
* Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied.
* If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit]
* elements will be appended, followed by the [truncated] string (which defaults to "...").
*/
public fun <T, A : Appendable> Sequence<T>.joinTo(buffer: A, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): A {
buffer.append(prefix)
var count = 0
for (element in this) {
if (++count > 1) buffer.append(separator)
if (limit < 0 || count <= limit) {
val text = if (element == null) "null" else element.toString()
buffer.append(text)
} else break
}
if (limit >= 0 && count > limit) buffer.append(truncated)
buffer.append(postfix)
return buffer
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied.
* If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit]
@@ -319,6 +341,17 @@ public fun <T> Iterable<T>.joinToString(separator: String = ", ", prefix: String
return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated).toString()
}
/**
* Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied.
* If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit]
* elements will be appended, followed by the [truncated] string (which defaults to "...").
*/
public fun <T> Sequence<T>.joinToString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String {
return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated).toString()
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied.
* If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit]