Creating a Scala Iterator for a Paged API

I am writing a Scala library to simplify a request using a paginated JSON API. Each API call returns an object that looks something like this:

{ "count": 100, "current_page": 1, "total_pages": 2, "records": [ ... ] } 

I would like to have a function that returns some kind of iterator such as MyIterator [Record]. Are there standard ways to do this in the Scala world, perhaps even a standard library that can help me?

I use lift-json for my JSON parsing if this is useful.

Thanks.

+4
source share
1 answer

If you have Iterator[Iterator[A]] , you can use flatten to create Iterator[A] , which combines all nested iterators:

 scala> Iterator(Iterator(1, 2), Iterator(3, 4)).flatten.toList res0: List[Int] = List(1, 2, 3, 4) 

Combined with one of the factory methods on a companion Iterator object

+5
source

Source: https://habr.com/ru/post/1337477/


All Articles