Skip to content

Instantly share code, notes, and snippets.

@Tagakov
Created May 31, 2018 18:01
Show Gist options
  • Save Tagakov/e7f35fb9b7da7d4a8ed7f081668c168d to your computer and use it in GitHub Desktop.
Save Tagakov/e7f35fb9b7da7d4a8ed7f081668c168d to your computer and use it in GitHub Desktop.
class StupidIterator(val source: Iterator<*>): Iterator<String> {
private var nextElement: Any? = null
init {
acquireNextElement()
}
private fun acquireNextElement() {
nextElement = if (source.hasNext()) source.next().let { if (it is Iterator<*>) StupidIterator(it) else it } else null
}
override fun hasNext(): Boolean {
if (nextElement != null) {
if (nextElement is Iterator<*>) {
if ((nextElement as Iterator<*>).hasNext()) {
return true
}
acquireNextElement()
return hasNext()
}
return true
}
return false
}
override fun next(): String {
if (nextElement is Iterator<*>) {
val iterator = nextElement as Iterator<*>
return iterator.next().also { if (!iterator.hasNext()) acquireNextElement() } as String
}
return nextElement.also { acquireNextElement() } as String
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment