a
This commit is contained in:
parent
218bb17cc4
commit
98e7ea96b2
@ -25,8 +25,7 @@ import java.util.stream.StreamSupport
|
||||
|
||||
// Aside parallel work, unimplemented Stream API elements can be easily implemented when required
|
||||
|
||||
private class FilteringIterator<T>(private val parent: Iterator<T>, private val predicate: Predicate<in T>) : MutableIterator<T> {
|
||||
private var value: T = parent.next()
|
||||
private class FilteringIterator<T>(private val parent: Iterator<T>, private val predicate: Predicate<in T>, private var value: T) : MutableIterator<T> {
|
||||
private var hasValue = true
|
||||
private var returned = false
|
||||
|
||||
@ -78,23 +77,26 @@ private class MappingIterator<T, R>(private val parent: Iterator<T>, private val
|
||||
private class FlatMappingIterator<T, R>(private val parent: Iterator<T>, private val mapper: (T) -> Iterator<R>) : MutableIterator<R> {
|
||||
private var current: Iterator<R> = mapper.invoke(parent.next())
|
||||
private var last: Iterator<R>? = null
|
||||
private var hasNext = current.hasNext()
|
||||
|
||||
init {
|
||||
while (!current.hasNext() && parent.hasNext()) {
|
||||
current = mapper.invoke(parent.next())
|
||||
}
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean {
|
||||
return hasNext
|
||||
return current.hasNext()
|
||||
}
|
||||
|
||||
override fun next(): R {
|
||||
if (!hasNext)
|
||||
if (!current.hasNext())
|
||||
throw NoSuchElementException()
|
||||
|
||||
val v = current.next()
|
||||
last = current
|
||||
hasNext = false
|
||||
|
||||
while (!hasNext && parent.hasNext()) {
|
||||
while (!current.hasNext() && parent.hasNext()) {
|
||||
current = mapper.invoke(parent.next())
|
||||
hasNext = current.hasNext()
|
||||
}
|
||||
|
||||
return v
|
||||
@ -183,11 +185,15 @@ fun <T> concatIterators(vararg iterators: Iterator<T>): MutableIterator<T> {
|
||||
* Resulting [Iterator] is [MutableIterator] if [this] is
|
||||
*/
|
||||
fun <T> Iterator<T>.filter(condition: Predicate<in T>): MutableIterator<T> {
|
||||
if (!hasNext()) {
|
||||
return emptyIterator()
|
||||
while (hasNext()) {
|
||||
val v = next()
|
||||
|
||||
if (condition.test(v)) {
|
||||
return FilteringIterator(this, condition, v)
|
||||
}
|
||||
}
|
||||
|
||||
return FilteringIterator(this, condition)
|
||||
return emptyIterator()
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user