StdLib cleanup: drop deprecated iterators and streams.

This commit is contained in:
Ilya Gorbunov
2015-06-26 00:09:38 +03:00
parent 6d4e48ab9a
commit 4660b07687
23 changed files with 7 additions and 2320 deletions
@@ -17,14 +17,6 @@ public fun <T> MutableCollection<in T>.addAll(sequence: Sequence<T>) {
for (item in sequence) add(item)
}
/**
* Adds all elements of the given [sequence] to this [MutableCollection].
*/
deprecated("Use Sequence<T> instead of Stream<T>")
public fun <T> MutableCollection<in T>.addAll(stream: Stream<T>) {
for (item in stream) add(item)
}
/**
* Adds all elements of the given [array] to this [MutableCollection].
*/
@@ -49,14 +41,6 @@ public fun <T> MutableCollection<in T>.removeAll(sequence: Sequence<T>) {
for (item in sequence) remove(item)
}
/**
* Removes all elements of the given [stream] from this [MutableCollection].
*/
deprecated("Use Sequence<T> instead of Stream<T>")
public fun <T> MutableCollection<in T>.removeAll(stream: Stream<T>) {
for (item in stream) remove(item)
}
/**
* Removes all elements of the given [array] from this [MutableCollection].
*/
@@ -21,14 +21,6 @@ public fun <T> Sequence<Sequence<T>>.flatten(): Sequence<T> {
return MultiSequence(this)
}
/**
* Returns a sequence of all elements from all sequences in this sequence.
*/
deprecated("Use Sequence<T> instead of Stream<T>")
public fun <T> Stream<Stream<T>>.flatten(): Stream<T> {
return FlatteningStream(this, { it })
}
/**
* Returns a single list of all elements from all arrays in the given array.
*/
@@ -3,35 +3,17 @@ package kotlin
import java.util.*
import kotlin.support.AbstractIterator
deprecated("Use Sequence<T> instead.")
public interface Stream<out T> {
/**
* Returns an iterator that returns the values from the sequence.
*/
public fun iterator(): Iterator<T>
}
/**
* A sequence that returns values through its iterator. The values are evaluated lazily, and the sequence
* is potentially infinite.
*
* @param T the type of elements in the sequence.
*/
public interface Sequence<out T> : Stream<T>
/**
* Converts a stream to a sequence.
*/
public fun<T> Stream<T>.toSequence(): Sequence<T> = object : Sequence<T> {
override fun iterator(): Iterator<T> = this@toSequence.iterator()
}
deprecated("Use sequenceOf() instead", ReplaceWith("sequenceOf(*elements)"))
public fun <T> streamOf(vararg elements: T): Stream<T> = elements.stream()
deprecated("Use sequenceOf() instead", ReplaceWith("sequenceOf(progression)"))
public fun <T> streamOf(progression: Progression<T>): Stream<T> = object : Stream<T> {
override fun iterator(): Iterator<T> = progression.iterator()
public interface Sequence<out T> {
/**
* Returns an iterator that returns the values from the sequence.
*/
public fun iterator(): Iterator<T>
}
/**
@@ -70,17 +52,10 @@ public fun <T> sequenceOf(progression: Progression<T>): Sequence<T> = object : S
*/
public fun <T> emptySequence(): Sequence<T> = EmptySequence
deprecated("Remove in M13 with streams.")
private fun <T> emptyStream(): Stream<T> = EmptySequence
private object EmptySequence : Sequence<Nothing> {
override fun iterator(): Iterator<Nothing> = EmptyIterator
}
deprecated("Use FilteringSequence<T> instead")
public class FilteringStream<T>(stream: Stream<T>, sendWhen: Boolean = true, predicate: (T) -> Boolean)
: Stream<T> by FilteringSequence<T>(stream.toSequence(), sendWhen, predicate)
/**
* A sequence that returns the values from the underlying [sequence] that either match or do not match
* the specified [predicate].
@@ -130,10 +105,6 @@ public class FilteringSequence<T>(private val sequence: Sequence<T>,
}
}
deprecated("Use TransformingSequence<T> instead")
public class TransformingStream<T, R>(stream: Stream<T>, transformer: (T) -> R)
: Stream<R> by TransformingSequence<T, R>(stream.toSequence(), transformer)
/**
* A sequence which returns the results of applying the given [transformer] function to the values
* in the underlying [sequence].
@@ -154,10 +125,6 @@ constructor(private val sequence: Sequence<T>, private val transformer: (T) -> R
}
}
deprecated("Use TransformingIndexedSequence<T> instead")
public class TransformingIndexedStream<T, R>(stream: Stream<T>, transformer: (Int, T) -> R)
: Stream<R> by TransformingIndexedSequence<T, R>(stream.toSequence(), transformer)
/**
* A sequence which returns the results of applying the given [transformer] function to the values
* in the underlying [sequence], where the transformer function takes the index of the value in the underlying
@@ -180,10 +147,6 @@ constructor(private val sequence: Sequence<T>, private val transformer: (Int, T)
}
}
deprecated("Use IndexingSequence<T> instead")
public class IndexingStream<T>(stream: Stream<T>)
: Stream<IndexedValue<T>> by IndexingSequence(stream.toSequence())
/**
* A sequence which combines values from the underlying [sequence] with their indices and returns them as
* [IndexedValue] objects.
@@ -205,10 +168,6 @@ constructor(private val sequence: Sequence<T>) : Sequence<IndexedValue<T>> {
}
}
deprecated("Use MergingSequence<T> instead")
public class MergingStream<T1, T2, V>(stream1: Stream<T1>, stream2: Stream<T2>, transform: (T1, T2) -> V)
: Stream<V> by MergingSequence(stream1.toSequence(), stream2.toSequence(), transform)
/**
* A sequence which takes the values from two parallel underlying sequences, passes them to the given
* [transform] function and returns the values returned by that function. The sequence stops returning
@@ -234,10 +193,6 @@ deprecated("This class is an implementation detail and shall be made internal so
}
}
deprecated("Use FlatteningSequence<T> instead")
public class FlatteningStream<T, R>(stream: Stream<T>, transformer: (T) -> Stream<R>)
: Stream<R> by FlatteningSequence(stream.toSequence(), { transformer(it).toSequence() })
deprecated("This class is an implementation detail and shall be made internal soon. Use sequence.flatMap() instead.")
public class FlatteningSequence<T, R>
deprecated("This class is an implementation detail and shall be made internal soon.", ReplaceWith("sequence.flatMap(transformer)"))
@@ -279,10 +234,6 @@ deprecated("This class is an implementation detail and shall be made internal so
}
}
deprecated("Use MultiSequence<T> instead")
public class Multistream<T>(stream: Stream<Stream<T>>)
: Stream<T> by FlatteningSequence(stream.toSequence(), { it.toSequence() })
deprecated("This class is an implementation detail and shall be made internal soon. Use sequence.flatten() instead.")
public class MultiSequence<T>
deprecated("This class is an implementation detail and shall be made internal soon.", ReplaceWith("sequence.flatten()"))
@@ -322,10 +273,6 @@ constructor(private val sequence: Sequence<Sequence<T>>) : Sequence<T> {
}
}
deprecated("Use TakeSequence<T> instead")
public class TakeStream<T>(stream: Stream<T>, count: Int)
: Stream<T> by TakeSequence(stream.toSequence(), count)
/**
* A sequence that returns at most [count] values from the underlying [sequence], and stops returning values
* as soon as that count is reached.
@@ -357,9 +304,6 @@ deprecated("This class is an implementation detail and shall be made internal so
}
}
deprecated("Use TakeWhileSequence<T> instead")
public class TakeWhileStream<T>(stream: Stream<T>, predicate: (T) -> Boolean) : Stream<T> by TakeWhileSequence<T>(stream.toSequence(), predicate)
/**
* A sequence that returns values from the underlying [sequence] while the [predicate] function returns
* `true`, and stops returning values once the function returns `false` for the next element.
@@ -408,10 +352,6 @@ deprecated("This class is an implementation detail and shall be made internal so
}
}
deprecated("Use DropSequence<T> instead")
public class DropStream<T>(stream: Stream<T>, count: Int)
: Stream<T> by DropSequence<T>(stream.toSequence(), count)
/**
* A sequence that skips the specified number of values from the underlying [sequence] and returns
* all values after that.
@@ -450,9 +390,6 @@ deprecated("This class is an implementation detail and shall be made internal so
}
}
deprecated("Use DropWhileSequence<T> instead")
public class DropWhileStream<T>(stream: Stream<T>, predicate: (T) -> Boolean) : Stream<T> by DropWhileSequence<T>(stream.toSequence(), predicate)
/**
* A sequence that skips the values from the underlying [sequence] while the given [predicate] returns `true` and returns
* all values after that.
@@ -502,9 +439,6 @@ deprecated("This class is an implementation detail and shall be made internal so
}
}
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)
}
@@ -624,9 +558,6 @@ public fun <T : Any> sequence(nextFunction: () -> T?): Sequence<T> {
return GeneratorSequence(nextFunction, { nextFunction() }).constrainOnce()
}
deprecated("Use sequence() instead", ReplaceWith("sequence(nextFunction)"))
public fun <T : Any> stream(nextFunction: () -> T?): Sequence<T> = sequence(nextFunction)
/**
* Returns a sequence which invokes the function to calculate the next value based on the previous one on each iteration
* until the function returns `null`. The sequence starts with the specified [initialValue].
@@ -643,6 +574,3 @@ public /*inline*/ fun <T : Any> sequence(initialValue: T, nextFunction: (T) -> T
*/
public fun <T: Any> sequence(initialValueFunction: () -> T?, nextFunction: (T) -> T?): Sequence<T> =
GeneratorSequence(initialValueFunction, nextFunction)
deprecated("Use sequence() instead", ReplaceWith("sequence(initialValue, nextFunction)"))
public /*inline*/ fun <T : Any> stream(initialValue: T, nextFunction: (T) -> T?): Sequence<T> = sequence(initialValue, nextFunction)
@@ -1,225 +0,0 @@
package kotlin
import kotlin.support.*
import java.util.Collections
import kotlin.test.assertTrue
/**
* Returns an iterator which invokes the function to calculate the next value on each iteration until the function returns *null*
*/
deprecated("Use sequence(...) function to make lazy sequence of values.")
public fun <T:Any> iterate(nextFunction: () -> T?) : Iterator<T> {
return FunctionIterator(nextFunction)
}
/**
* Returns an iterator which invokes the function to calculate the next value based on the previous one on each iteration
* until the function returns *null*
*/
deprecated("Use sequence(...) function to make lazy sequence of values.")
public /*inline*/ fun <T: Any> iterate(initialValue: T, nextFunction: (T) -> T?): Iterator<T> =
iterate(nextFunction.toGenerator(initialValue))
/**
* Returns an iterator whose values are pairs composed of values produced by given pair of iterators
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public fun <T, S> Iterator<T>.zip(iterator: Iterator<S>): Iterator<Pair<T, S>> = PairIterator(this, iterator)
/**
* Returns an iterator shifted to right by the given number of elements
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public fun <T> Iterator<T>.skip(n: Int): Iterator<T> = SkippingIterator(this, n)
deprecated("Use FilteringStream<T> instead")
public class FilterIterator<T>(private val iterator: Iterator<T>, private val predicate: (T) -> Boolean) :
AbstractIterator<T>() {
override protected fun computeNext(): Unit {
while (iterator.hasNext()) {
val next = iterator.next()
if ((predicate)(next)) {
setNext(next)
return
}
}
done()
}
}
deprecated("Use FilteringStream<T> instead")
public class FilterNotNullIterator<T : Any>(private val iterator: Iterator<T?>?) : AbstractIterator<T>() {
override protected fun computeNext(): Unit {
if (iterator != null) {
while (iterator.hasNext()) {
val next = iterator.next()
if (next != null) {
setNext(next)
return
}
}
}
done()
}
}
deprecated("Use TransformingStream<T> instead")
public class MapIterator<T, R>(private val iterator: Iterator<T>, private val transform: (T) -> R) :
AbstractIterator<R>() {
override protected fun computeNext(): Unit {
if (iterator.hasNext()) {
setNext((transform)(iterator.next()))
} else {
done()
}
}
}
deprecated("Use FlatteningStream<T> instead")
public class FlatMapIterator<T, R>(private val iterator: Iterator<T>, private val transform: (T) -> Iterator<R>) :
AbstractIterator<R>() {
private var transformed: Iterator<R> = iterate<R> { null }
override protected fun computeNext(): Unit {
while (true) {
if (transformed.hasNext()) {
setNext(transformed.next())
return
}
if (iterator.hasNext()) {
transformed = (transform)(iterator.next())
} else {
done()
return
}
}
}
}
deprecated("Use LimitedStream<T> instead")
public class TakeWhileIterator<T>(private val iterator: Iterator<T>, private val predicate: (T) -> Boolean) :
AbstractIterator<T>() {
override protected fun computeNext() : Unit {
if (iterator.hasNext()) {
val item = iterator.next()
if ((predicate)(item)) {
setNext(item)
return
}
}
done()
}
}
/** An [[Iterator]] which invokes a function to calculate the next value in the iteration until the function returns *null* */
deprecated("Use FunctionStream<T> instead")
public class FunctionIterator<T : Any>(private val nextFunction: () -> T?) : AbstractIterator<T>() {
override protected fun computeNext(): Unit {
val next = (nextFunction)()
if (next == null) {
done()
} else {
setNext(next)
}
}
}
/** An [[Iterator]] which iterates over a number of iterators in sequence */
deprecated("Use Multistream<T> instead")
public fun CompositeIterator<T>(vararg iterators: Iterator<T>): CompositeIterator<T> = CompositeIterator(iterators.iterator())
deprecated("Use Multistream<T> instead")
public class CompositeIterator<T>(private val iterators: Iterator<Iterator<T>>) : AbstractIterator<T>() {
private var currentIter: Iterator<T>? = null
override protected fun computeNext(): Unit {
while (true) {
if (currentIter == null) {
if (iterators.hasNext()) {
currentIter = iterators.next()
} else {
done()
return
}
}
val iter = currentIter
if (iter != null) {
if (iter.hasNext()) {
setNext(iter.next())
return
} else {
currentIter = null
}
}
}
}
}
/** A singleton [[Iterator]] which invokes once over a value */
deprecated("Use streams for lazy collection operations.")
public class SingleIterator<T>(private val value: T) : AbstractIterator<T>() {
private var first = true
override protected fun computeNext(): Unit {
if (first) {
first = false
setNext(value)
} else {
done()
}
}
}
deprecated("Use streams for lazy collection operations.")
public class IndexIterator<T>(private val iterator: Iterator<T>) : Iterator<Pair<Int, T>> {
private var index: Int = 0
override fun next(): Pair<Int, T> {
return Pair(index++, iterator.next())
}
override fun hasNext(): Boolean {
return iterator.hasNext()
}
}
deprecated("Use ZippingStream<T> instead.")
public class PairIterator<T, S>(
private val iterator1: Iterator<T>, private val iterator2: Iterator<S>
): AbstractIterator<Pair<T, S>>() {
protected override fun computeNext() {
if (iterator1.hasNext() && iterator2.hasNext()) {
setNext(Pair(iterator1.next(), iterator2.next()))
}
else {
done()
}
}
}
deprecated("Use streams for lazy collection operations.")
public class SkippingIterator<T>(private val iterator: Iterator<T>, private val n: Int) : Iterator<T> {
private var firstTime: Boolean = true
private fun skip() {
for (i in 1..n) {
if (!iterator.hasNext()) break
iterator.next()
}
firstTime = false
}
override fun next(): T {
assertTrue(!firstTime, "hasNext() must be invoked before advancing an iterator")
return iterator.next()
}
override fun hasNext(): Boolean {
if (firstTime) {
skip()
}
return iterator.hasNext()
}
}
@@ -1,23 +0,0 @@
package kotlin
import kotlin.support.*
/** Returns an iterator over elements that are instances of a given type *R* which is a subclass of *T* */
deprecated("Use streams for lazy collection operations.")
public fun <T, R : T> Iterator<T>.filterIsInstance(klass: Class<R>): Iterator<R> = FilterIsIterator<T, R>(this, klass)
deprecated("Use streams for lazy collection operations.")
public class FilterIsIterator<T, R : T>(private val iterator: Iterator<T>, private val klass: Class<R>) :
AbstractIterator<R>() {
override protected fun computeNext(): Unit {
while (iterator.hasNext()) {
val next = iterator.next()
if (klass.isInstance(next)) {
setNext(next as R)
return
}
}
done()
}
}
@@ -1,492 +0,0 @@
package kotlin
import java.util.*
import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js
/**
* Returns *true* if all elements match the given *predicate*
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public inline fun <T> Iterator<T>.all(predicate: (T) -> Boolean) : Boolean {
for (element in this) if (!predicate(element)) return false
return true
}
/**
* Returns *true* if any elements match the given *predicate*
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public inline fun <T> Iterator<T>.any(predicate: (T) -> Boolean) : Boolean {
for (element in this) if (predicate(element)) return true
return false
}
/**
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public fun <T> Iterator<T>.appendString(buffer: Appendable, separator: String = ", ", prefix: String ="", postfix: String = "", limit: Int = -1, truncated: String = "...") : Unit {
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)
}
/**
* Returns the number of elements which match the given *predicate*
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public inline fun <T> Iterator<T>.count(predicate: (T) -> Boolean) : Int {
var count = 0
for (element in this) if (predicate(element)) count++
return count
}
/**
* Returns a list containing everything but the first *n* elements
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public fun <T> Iterator<T>.drop(n: Int) : List<T> {
return dropWhile(countTo(n))
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public inline fun <T> Iterator<T>.dropWhile(predicate: (T) -> Boolean) : List<T> {
return dropWhileTo(ArrayList<T>(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public inline fun <T, L: MutableList<in T>> Iterator<T>.dropWhileTo(result: L, predicate: (T) -> Boolean) : L {
var start = true
for (element in this) {
if (start && predicate(element)) {
// ignore
} else {
start = false
result.add(element)
}
}
return result
}
/**
* Returns an iterator over elements which match the given *predicate*
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public fun <T> Iterator<T>.filter(predicate: (T) -> Boolean) : Iterator<T> {
return FilterIterator<T>(this, predicate)
}
/**
* Returns an iterator over elements which don't match the given *predicate*
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public inline fun <T> Iterator<T>.filterNot(inlineOptions(InlineOption.ONLY_LOCAL_RETURN) predicate: (T) -> Boolean) : Iterator<T> {
return filter {!predicate(it)}
}
/**
* Returns an iterator over non-*null* elements
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public fun <T:Any> Iterator<T?>.filterNotNull() : Iterator<T> {
return FilterNotNullIterator(this)
}
/**
* Filters all non-*null* elements into the given list
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public fun <T:Any, C: MutableCollection<in T>> Iterator<T?>.filterNotNullTo(result: C) : C {
for (element in this) if (element != null) result.add(element)
return result
}
/**
* Returns a list containing all elements which do not match the given *predicate*
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public inline fun <T, C: MutableCollection<in T>> Iterator<T>.filterNotTo(result: C, predicate: (T) -> Boolean) : C {
for (element in this) if (!predicate(element)) result.add(element)
return result
}
/**
* Filters all elements which match the given predicate into the given list
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public inline fun <T, C: MutableCollection<in T>> Iterator<T>.filterTo(result: C, predicate: (T) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element)
return result
}
/**
* Returns the first element which matches the given *predicate* or *null* if none matched
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public inline fun <T:Any> Iterator<T>.find(predicate: (T) -> Boolean) : T? {
for (element in this) if (predicate(element)) return element
return null
}
/**
* Returns an iterator over the concatenated results of transforming each element to one or more values
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public fun <T, R> Iterator<T>.flatMap(transform: (T) -> Iterator<R>) : Iterator<R> {
return FlatMapIterator<T, R>(this, transform)
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single collection
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public inline fun <T, R, C: MutableCollection<in R>> Iterator<T>.flatMapTo(result: C, transform: (T) -> Iterable<R>) : C {
for (element in this) {
val list = transform(element)
for (r in list) result.add(r)
}
return result
}
/**
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public inline fun <T, R> Iterator<T>.fold(initial: R, operation: (R, T) -> R) : R {
var answer = initial
for (element in this) answer = operation(answer, element)
return answer
}
/**
* Performs the given *operation* on each element
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public inline fun <T> Iterator<T>.forEach(operation: (T) -> Unit) : Unit {
for (element in this) operation(element)
}
/**
* Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public inline fun <T, K> Iterator<T>.groupBy(toKey: (T) -> K) : Map<K, List<T>> {
return groupByTo(HashMap<K, MutableList<T>>(), toKey)
}
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public inline fun <T, K> Iterator<T>.groupByTo(result: MutableMap<K, MutableList<T>>, toKey: (T) -> K) : Map<K, MutableList<T>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<T>() }
list.add(element)
}
return result
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public fun <T> Iterator<T>.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...") : String {
val buffer = StringBuilder()
appendString(buffer, separator, prefix, postfix, limit, truncated)
return buffer.toString()
}
// -------------------------
/**
* Returns an iterator obtained by applying *transform*, a function transforming an object of type *T* into an object of type *R*
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public fun <T, R> Iterator<T>.map(transform : (T) -> R) : Iterator<R> {
return MapIterator<T, R>(this, transform)
}
/**
* Transforms each element of this collection with the given *transform* function and
* adds each return value to the given *results* collection
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public inline fun <T, R, C: MutableCollection<in R>> Iterator<T>.mapTo(result: C, transform : (T) -> R) : C {
for (item in this)
result.add(transform(item))
return result
}
/**
* Returns the largest element or null if there are no elements
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public fun <T: Comparable<T>> Iterator<T>.max() : T? {
if (!hasNext()) return null
var max = next()
while (hasNext()) {
val e = next()
if (max < e) max = e
}
return max
}
/**
* Returns the first element yielding the largest value of the given function or null if there are no elements
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public inline fun <R: Comparable<R>, T: Any> Iterator<T>.maxBy(f: (T) -> R) : T? {
if (!hasNext()) return null
var maxElem = next()
var maxValue = f(maxElem)
while (hasNext()) {
val e = next()
val v = f(e)
if (maxValue < v) {
maxElem = e
maxValue = v
}
}
return maxElem
}
/**
* Returns the smallest element or null if there are no elements
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public fun <T: Comparable<T>> Iterator<T>.min() : T? {
if (!hasNext()) return null
var min = next()
while (hasNext()) {
val e = next()
if (min > e) min = e
}
return min
}
/**
* Returns the first element yielding the smallest value of the given function or null if there are no elements
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public inline fun <R: Comparable<R>, T: Any> Iterator<T>.minBy(f: (T) -> R) : T? {
if (!hasNext()) return null
var minElem = next()
var minValue = f(minElem)
while (hasNext()) {
val e = next()
val v = f(e)
if (minValue > v) {
minElem = e
minValue = v
}
}
return minElem
}
/**
* Partitions this collection into a pair of collections
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public inline fun <T> Iterator<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)
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the following collection
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public fun <T> Iterator<T>.plus(collection: Iterable<T>) : Iterator<T> {
return plus(collection.iterator())
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the given element at the end
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public fun <T> Iterator<T>.plus(element: T) : Iterator<T> {
return CompositeIterator<T>(this, SingleIterator(element))
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the following iterator
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public fun <T> Iterator<T>.plus(iterator: Iterator<T>) : Iterator<T> {
return CompositeIterator<T>(this, iterator)
}
/**
* Applies binary operation to all elements of iterable, going from left to right.
* Similar to fold function, but uses the first element as initial value
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public inline fun <T> Iterator<T>.reduce(operation: (T, T) -> T) : T {
val iterator = this.iterator()
if (!iterator.hasNext()) {
throw UnsupportedOperationException("Empty iterable can't be reduced")
}
var result: T = iterator.next() //compiler doesn't understand that result will initialized anyway
while (iterator.hasNext()) {
result = operation(result, iterator.next())
}
return result
}
/**
* Returns a original Iterable containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public fun <T:Any> Iterator<T?>.requireNoNulls() : Iterator<T> {
return map<T?, T>{
if (it == null) throw IllegalArgumentException("null element in iterator $this") else it
}
}
/**
* Reverses the order the elements into a list
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public fun <T> Iterator<T>.reverse() : List<T> {
val list = toCollection(ArrayList<T>())
Collections.reverse(list)
return list
}
/**
* Copies all elements into a [[List]] and sorts it by value of compare_function(element)
* E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public inline fun <T, R: Comparable<R>> Iterator<T>.sortBy(inlineOptions(InlineOption.ONLY_LOCAL_RETURN) f: (T) -> R) : List<T> {
val sortedList = toCollection(ArrayList<T>())
val sortBy: Comparator<T> = comparator<T> {x: T, y: T ->
val xr = f(x)
val yr = f(y)
xr.compareTo(yr)
}
java.util.Collections.sort(sortedList, sortBy)
return sortedList
}
/**
* Returns an iterator restricted to the first *n* elements
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public fun <T> Iterator<T>.take(n: Int) : Iterator<T> {
var count = n
return takeWhile{ --count >= 0 }
}
/**
* Returns an iterator restricted to the first elements that match the given *predicate*
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public fun <T> Iterator<T>.takeWhile(predicate: (T) -> Boolean) : Iterator<T> {
return TakeWhileIterator<T>(this, predicate)
}
/**
* Returns a list containing the first elements that satisfy the given *predicate*
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public inline fun <T, C: MutableCollection<in T>> Iterator<T>.takeWhileTo(result: C, predicate: (T) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element) else break
return result
}
/**
* Copies all elements into the given collection
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public fun <T, C: MutableCollection<in T>> Iterator<T>.toCollection(result: C) : C {
for (element in this) result.add(element)
return result
}
/**
* Copies all elements into a [[LinkedList]]
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public fun <T> Iterator<T>.toLinkedList() : LinkedList<T> {
return toCollection(LinkedList<T>())
}
/**
* Copies all elements into a [[List]]
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public fun <T> Iterator<T>.toList() : List<T> {
return toCollection(ArrayList<T>())
}
/**
* Copies all elements into a [[ArrayList]]
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public fun <T> Iterator<T>.toArrayList() : ArrayList<T> {
return toCollection(ArrayList<T>())
}
/**
* Copies all elements into a [[Set]]
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public fun <T> Iterator<T>.toSet() : Set<T> {
return toCollection(LinkedHashSet<T>())
}
/**
* Copies all elements into a [[HashSet]]
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public fun <T> Iterator<T>.toHashSet() : HashSet<T> {
return toCollection(HashSet<T>())
}
/**
* Copies all elements into a [[SortedSet]]
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public fun <T> Iterator<T>.toSortedSet() : SortedSet<T> {
return toCollection(TreeSet<T>())
}
/**
* Returns an iterator of Pairs(index, data)
*/
deprecated("Replace Iterator<T> with Sequence<T> by using sequence() function instead of iterator()")
public fun <T> Iterator<T>.withIndices() : Iterator<Pair<Int, T>> {
return IndexIterator(iterator())
}
@@ -269,11 +269,6 @@ public fun Sequence<String>.join(separator: String = ", ", prefix: String = "",
return joinToString(separator, prefix, postfix, limit, truncated)
}
deprecated("Migrate to using Sequence<T> and respective functions")
public fun Stream<String>.join(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String {
return joinToString(separator, prefix, postfix, limit, truncated)
}
/**
* Returns a substring before the first occurrence of [delimiter].
* If the string does not contain the delimiter, returns [missingDelimiterValue] which defaults to the original string.