Skip to content

Instantly share code, notes, and snippets.

@kazup0n
Created December 5, 2018 13:54
Show Gist options
  • Save kazup0n/aed3178ed3daacc193910f753ca59c7c to your computer and use it in GitHub Desktop.
Save kazup0n/aed3178ed3daacc193910f753ca59c7c to your computer and use it in GitHub Desktop.
import scala.concurrent.Future
import scala.concurrent.ExecutionContext
implicit val ec = ExecutionContext.global
val ns = Seq(1,2,3,4,5,6,7,8,9,10)
def makeF(n:Int):Future[Int] = Future.successful(n)
def sum(numbers:Seq[Int]):Future[Int] = {
def go(t:(Seq[Int], Int)):Future[(Seq[Int], Int)] = {
println(t._2)
t._1 match {
case Seq() => Future.successful(t)
case Seq(h) => makeF(h).map(n => (Seq(), t._2+n))
case h::tail => makeF(h).flatMap(s => go((tail, s+t._2)))
}
}
go((numbers, 0)).map(t => t._2)
}
sum(ns).map{n=>println(n); n}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment