Suppose you are using an API that allows you to scroll through a resultset in pages. Each page returns an ID for the subsequent page.
It's an established idiom that you can use Scala Iterator and its lazy concat (++) operator recursively to do this:
def allResults: Iterator[Result] = {
def nextPage(pageId: String): Iterator[Result] = {
val page = invoke api
Iterator(page.results) ++ nextPage(page.nextPageId)
}
val firstPage = invoke api
Iterator(firstPage.results) ++ nextPage(firstPage.nextPageId)
}
Is this idiom stack-safe? Or are there other efficiency gotchas to worry about?