Skip to content

Instantly share code, notes, and snippets.

@fiadliel
Last active August 29, 2015 14:24
Show Gist options
  • Save fiadliel/597c9a859682fb42747c to your computer and use it in GitHub Desktop.
Save fiadliel/597c9a859682fb42747c to your computer and use it in GitHub Desktop.
Paged API requests for scalaz-stream
case class Response(results: Seq[String], nextPage: Option[Int])
def getPagedUrl(url: String, curPage: Option[Int]): Task[Response] = ???
def pagedRequest(url: String, curPage: Option[Int] = None): Process[Task, String] = {
Process.eval(getPagedUrl(url, curPage)) flatMap { response =>
Process.emitAll(response.results) ++
response.nextPage.map(p => pagedRequest(url, Some(p))).getOrElse(Process.empty)
}
}
@suls
Copy link

suls commented Jul 12, 2015

Works like a charm!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment