added test case for KT-1646 in an attempt to implement lazy filter/flatMap for Iterator
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
package kotlin.util
|
package kotlin
|
||||||
|
|
||||||
import kotlin.support.AbstractIterator
|
import kotlin.support.AbstractIterator
|
||||||
|
|
||||||
@@ -9,7 +9,7 @@ import java.util.Iterator
|
|||||||
inline fun <T, R: T> java.util.Iterator<T>.filterIsInstance(klass: Class<R>): Iterator<R> = FilterIsIterator<T,R>(this, klass)
|
inline fun <T, R: T> java.util.Iterator<T>.filterIsInstance(klass: Class<R>): Iterator<R> = FilterIsIterator<T,R>(this, klass)
|
||||||
|
|
||||||
private class FilterIsIterator<T, R :T>(val iter: java.util.Iterator<T>, val klass: Class<R>) : AbstractIterator<R>() {
|
private class FilterIsIterator<T, R :T>(val iter: java.util.Iterator<T>, val klass: Class<R>) : AbstractIterator<R>() {
|
||||||
override fun computeNext(): R? {
|
override protected fun computeNext(): R? {
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
val next = iter.next()
|
val next = iter.next()
|
||||||
if (klass.isInstance(next)) {
|
if (klass.isInstance(next)) {
|
||||||
@@ -21,37 +21,46 @@ private class FilterIsIterator<T, R :T>(val iter: java.util.Iterator<T>, val kla
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// TODO when we're past the compiler error
|
||||||
|
// http://youtrack.jetbrains.com/issue/KT-1646
|
||||||
|
//
|
||||||
|
// we can use this code to replace the other
|
||||||
|
// filter / FilterIterator class as it reuses the
|
||||||
|
// core logic from AbstractIterator
|
||||||
|
|
||||||
/** Returns a new lazy iterator containing all elements in this iteration which match the given predicate */
|
/** Returns a new lazy iterator containing all elements in this iteration which match the given predicate */
|
||||||
/*
|
inline fun <T> java.util.Iterator<T>.filter2(predicate: (T)-> Boolean) : java.util.Iterator<T> {
|
||||||
inline fun <T> java.util.Iterator<T>.filter(predicate: (T)-> Boolean) : java.util.Iterator<T> {
|
return FilterIterator2(this, predicate)
|
||||||
return FilterIterator(this, predicate)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class FilterIterator<T>(val iter: java.util.Iterator<T>, val predicate: (T)-> Boolean) : AbstractIterator<T>() {
|
private class FilterIterator2<T>(val iter: java.util.Iterator<T>, val fn: (T)-> Boolean) : AbstractIterator<T>() {
|
||||||
override fun computeNext(): T? {
|
override protected fun computeNext(): T? {
|
||||||
|
/*
|
||||||
|
|
||||||
|
TODO this block generates a compiler error:
|
||||||
|
http://youtrack.jetbrains.com/issue/KT-1646
|
||||||
|
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
val next = iter.next()
|
val element = iter.next()
|
||||||
if (predicate(next)) {
|
if (fn(element)) {
|
||||||
return next
|
return element
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
done()
|
done()
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
/** Returns a new lazy iterator containing all elements in this iteration which do not match the given predicate */
|
/** Returns a new lazy iterator containing all elements in this iteration which do not match the given predicate */
|
||||||
/*
|
inline fun <T> java.util.Iterator<T>.filterNot(predicate: (T)-> Boolean) : java.util.Iterator<T> {
|
||||||
inline fun <T> java.util.Iterator<T>.filterNot(predicate: (T)-> Boolean) : java.util.Iterator<T> {
|
return filter {
|
||||||
return filter {
|
!predicate(it)
|
||||||
!predicate(it)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
*/
|
}
|
||||||
|
|
||||||
/** Returns a new lazy iterator containing all the non null elements in this iteration */
|
/** Returns a new lazy iterator containing all the non null elements in this iteration */
|
||||||
/*
|
|
||||||
inline fun <T> java.util.Iterator<T?>?.filterNotNull() : java.util.Iterator<T> = FilterNotNullIterator(this)
|
inline fun <T> java.util.Iterator<T?>?.filterNotNull() : java.util.Iterator<T> = FilterNotNullIterator(this)
|
||||||
|
|
||||||
private class FilterNotNullIterator<T>(val iter: java.util.Iterator<T?>?) : AbstractIterator<T>() {
|
private class FilterNotNullIterator<T>(val iter: java.util.Iterator<T?>?) : AbstractIterator<T>() {
|
||||||
@@ -68,7 +77,6 @@ private class FilterNotNullIterator<T>(val iter: java.util.Iterator<T?>?) : Abst
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -76,6 +84,9 @@ private class FilterNotNullIterator<T>(val iter: java.util.Iterator<T?>?) : Abst
|
|||||||
* are concatenated together into a single collection
|
* are concatenated together into a single collection
|
||||||
*/
|
*/
|
||||||
/*
|
/*
|
||||||
|
|
||||||
|
TODO implement a lazy flatMap
|
||||||
|
|
||||||
inline fun <T, R> java.util.Iterator<T>.flatMap(transform: (T)-> java.util.Iterator<R>) : Collection<R> {
|
inline fun <T, R> java.util.Iterator<T>.flatMap(transform: (T)-> java.util.Iterator<R>) : Collection<R> {
|
||||||
return flatMapTo<>(ArrayList<R>(), transform)
|
return flatMapTo<>(ArrayList<R>(), transform)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user