Created
May 4, 2021 11:57
-
-
Save phdoerfler/557a4cc4031475def4a9143a9cebccbc to your computer and use it in GitHub Desktop.
WIP and improved version of https://stackoverflow.com/questions/67381862/consume-paginated-not-really-api-in-scala-with-unknown-page-count-and-unknow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.doerfler | |
/** | |
* As an alternative consider flatMapConcat and mapConcat of Akka Streams. | |
*/ | |
abstract class IteratorThatKeepsOnGiving[T] extends Iterator[T] { | |
private var currentIter: Iterator[T] = Iterator.empty[T] | |
private var currentElement: Option[T] = None | |
def nextIterator(lastElement: Option[T]): Iterator[T] | |
def fetchNewBatch(lastElement: Option[T]) = { | |
currentIter = nextIterator(lastElement) | |
} | |
def hasNext: Boolean = currentIter.hasNext || { | |
fetchNewBatch(currentElement) | |
currentIter.hasNext | |
} | |
def next(): T = { | |
currentIter.nextOption() match { | |
case v @ Some(value) => | |
currentElement = v | |
value | |
case None => | |
fetchNewBatch(currentElement) | |
next() | |
} | |
} | |
} | |
object IteratorThatKeepsOnGiving { | |
def apply[T](f: Option[T] => Iterator[T]) = new IteratorThatKeepsOnGiving[T] { | |
def nextIterator(lastElement: Option[T]): Iterator[T] = f(lastElement) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment