Created
December 5, 2018 13:54
-
-
Save kazup0n/aed3178ed3daacc193910f753ca59c7c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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