Skip to content

Instantly share code, notes, and snippets.

@davegurnell
Created September 3, 2016 12:36
Show Gist options
  • Select an option

  • Save davegurnell/5c62365ac9b17b9ce32e301b4b1435fb to your computer and use it in GitHub Desktop.

Select an option

Save davegurnell/5c62365ac9b17b9ce32e301b4b1435fb to your computer and use it in GitHub Desktop.
Snippet for a conversation in the Cats Gitter channel
import scala.annotation.tailrec
import scala.concurrent.{ ExecutionContext => EC, _ }
trait Json // whatever JSON type you're using
def getJson(url: String)(implicit ec: EC): Future[Json] =
??? // whatever implementation you currently have
def getNextUrlFromJson(json: Json): Option[String] =
??? // whatever implementation you currently have
def getAllJson(url: String)(implicit ec: EC): Future[Seq[Json]] =
getJson(url) flatMap { json =>
getNextUrlFromJson(json) match {
case Some(nextUrl) => getAllJson(nextUrl).map(rest => json +: rest)
case None => Future.successful(Seq.empty)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment