Skip to content

Instantly share code, notes, and snippets.

@rubanm
Created January 3, 2014 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubanm/8245660 to your computer and use it in GitHub Desktop.
Save rubanm/8245660 to your computer and use it in GitHub Desktop.
Paging responses - avoid while loop, use Stream takeWhile. Somewhat ugly, but works.
val responses = MutableList[Response]()
Stream.from(0, PAGE_SIZE).takeWhile { case offset =>
request.setOffset(offset)
val response = Await.result(client.execute(request))
response.getResponseCode match {
case ResponseCode.OK => responses += (response); true
case ResponseCode.END => false
case _ => false // or throw exception
}
}.foreach { case exec => () } // not lazy
responses.reduce { ... }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment