Why this simple Scala for comprehension does not execute the futures? - scala

I'm stuck figuring out why this does not work:
import scala.concurrent.future
import scala.concurrent.Future
import scala.concurrent.ExecutionContext
import scala.concurrent.ExecutionContext.Implicits.global
object FutureTest {
def main(args: Array[String]) {
val result1 = future("a")
val result2 = future("b")
val result3 = future("c")
val res = for {
r1 <- result1
r2 <- result2
r3 <- result3
} yield (r1 + r2 + r3)
for { r <- res} yield(println(r))
}
}
I'm expecting this to print "abc", but nothing really happens.

You are executing a stand alone program and the problem is that the main thread is terminated before the future can complete, to see something you could use this:
import scala.concurrent.future
import scala.concurrent.Future
import scala.concurrent.ExecutionContext
import scala.concurrent.ExecutionContext.Implicits.global
object FutureTest {
def main(args: Array[String]) {
val result1 = future("a")
val result2 = future("b")
val result3 = future("c")
val res = for {
r1 <- result1
r2 <- result2
r3 <- result3
} yield (r1 + r2 + r3)
val printing = for { r <- res} yield(println(r))
Await.ready(printing, Duration.Inf)
}
}

Related

Does cats mapN run all the futures in parallel?

As mentioned in the jump-start guide, mapN will run all the futures in parallel, so I created the below simple Scala program, but a sample run shows diff to be 9187 ms and diffN to be 9106 ms. So it looks like that the mapN is also running the futures sequentially, isn't it? Please let me know if I am missing something?
package example
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import java.time.LocalDateTime
import java.time.Duration
import scala.util.Failure
import scala.util.Success
import java.time.ZoneOffset
import cats.instances.future._
import cats.syntax.apply._
object FutureEx extends App {
val before = LocalDateTime.now()
val sum = for {
a <- getA
b <- getB
c <- getC
} yield (a + b + c)
sum onComplete {
case Failure(ex) => println(s"Error: ${ex.getMessage()}")
case Success(value) =>
val after = LocalDateTime.now()
println(s"before: $before")
println(s"after: $after")
val diff = getDiff(before, after)
println(s"diff: $diff")
println(s"sum: $value")
}
// let the above finish
Thread.sleep(20000)
val beforeN = LocalDateTime.now()
val usingMapN = (getA, getB, getC).mapN(add)
usingMapN onComplete {
case Failure(ex) => println(s"Error: ${ex.getMessage()}")
case Success(value) =>
val afterN = LocalDateTime.now()
println(s"beforeN: $beforeN")
println(s"afterN: $afterN")
val diff = getDiff(beforeN, afterN)
println(s"diffN: $diff")
println(s"sum: $value")
}
def getA: Future[Int] = {
println("inside A")
Thread.sleep(3000)
Future.successful(2)
}
def getB: Future[Int] = {
println("inside B")
Thread.sleep(3000)
Future.successful(3)
}
def getC: Future[Int] = {
println("inside C")
Thread.sleep(3000)
Future.successful(4)
}
def add(a: Int, b: Int, c: Int) = a + b + c
def getDiff(before: LocalDateTime, after: LocalDateTime): Long = {
Duration.between(before.toInstant(ZoneOffset.UTC), after.toInstant(ZoneOffset.UTC)).toMillis()
}
}
Because you have sleep outside Future it should be like:
def getA: Future[Int] = Future {
println("inside A")
Thread.sleep(3000)
2
}
So you start async Future with apply - Future.successful on the other hand returns pure value, meaning you execute sleep in same thread.
The time is going before mapN is ever called.
(getA, getB, getC).mapN(add)
This expression is creating a tuple (sequentially) and then calling mapN on it. So it is calling getA then getB then getC and since each of them has a 3 second delay, it takes 9 seconds.

Scala: write for-comprehension with ReaderT and Option

Here is example:
trait Service1 { def s1f = Option(10) }
trait Service2 {
type ReaderS1[A] = ReaderT[Option,Service1,A]
def s2f1: ReaderS1[Int] =
ReaderT(s1 =>
for {
r1 <- Option(1)
r2 <- s1.s1f
} yield r1 + r2
)
}
It works fine. I just want to rewrite s2f1 without ReaderT.apply method:
def s2f2:ReaderS1[Int] =
for {
r1 <- 1.pure[ReaderS1]
r2 <- //how to get result of Service1.s1f and compose it here
} yield r1 + r2
Here is a working example with Reader[...,Int], but not ReaderT[Option,...]:
import cats.data.Reader
trait Service1 { def s1f = 10 }
trait Service2 { def s2f = 20 }
trait Service3 {
def s3f1:Reader[Service1,Int] = Reader(1 + _.s1f)
def s3f2:Reader[Service2,Int] = Reader(2 + _.s2f)
import cats.syntax.applicative._ //for pure
type Env = (Service1, Service2)
type ReaderEnv[A] = Reader[Env,A] //needed to convert Int via pure
def c:ReaderEnv[Int] =
for {
s1 <- Reader((_:Env)._1)
r2 <- s1.s1f.pure[ReaderEnv]
r1 <- s3f2.local((_:Env)._2)
} yield r1 + r2
}
I want to get a similar syntax.
Try
import cats.syntax.applicative._
import cats.instances.option._
def s2f2: ReaderS1[Int] =
for {
r1 <- 1.pure[ReaderS1]
r2 <- ReaderT((_: Service1).s1f)
} yield r1 + r2

Scala futures and Await.result()

I expect the following code to result in syncResult containing "string two", but instead, I get the error java.lang.NoClassDefFoundError: Could not initialize class.
import scala.concurrent.{Await, Future}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
def randomFunction1(): Future[String] = {
Future.successful("string one")
}
def randomFunction2(): Future[String] = {
Future.successful("string two")
}
val asyncResult: Future[String] = for {
r1 <- randomFunction1()
r2 <- randomFunction2()
} yield r2
val syncResult: String = Await.result(
asyncResult,
1.second
)
I get similar results with the following.
import scala.concurrent.{Await, Future}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
def randomFunction1(): Future[String] = {
Future.successful("string one")
}
def randomFunction2(): Future[String] = {
Future.successful("string two")
}
val asyncResult: Future[String] = randomFunction1().flatMap(
r1 => {
randomFunction2()
}
)
val syncResult: String = Await.result(
asyncResult,
1.second
)
I'm using the Scala 2.12.2 interpreter to run this using :paste.
What's wrong with my code?
This seemed to be an issue with the Scala REPL.

Why Scala Future returns None using flatMap and for comprehension?

I have following Scala code, used with Scala 2.10
package reactive
import scala.concurrent.future
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
object futuresum extends App {
def f1 = future { 12 }
def f2 = future { 13 }
val result1 = f1.flatMap(x => f2.map(y => x + y))
println(result1.value)
val result2 = for (x <- f1; y <- f2) yield (x + y)
println(result2.value)
}
This code should print 25, but it prints None:
None
None
What is wrong with this code?
EDIT
This works for me now:
package reactive
import scala.concurrent.future
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.concurrent.Await
import scala.concurrent.duration.Duration
object futuresum extends App {
def f1 = future { 12 }
def f2 = future { 13 }
val result1 = f1.flatMap(x => f2.map(y => x + y))
result1.onComplete(b => println(b))
val result2 = for (x <- f1; y <- f2) yield (x + y)
result2.onComplete(b => println(b))
Await.result(result1, Duration.Inf)
Await.result(result2, Duration.Inf)
}
My guess is that between the "result"s and its println the futures haven't had enough time to spawn/send-to their threads. Check this quick REPL session:
scala> f1.flatMap(x => f2.map(y => x + y)).value
res2: Option[scala.util.Try[Int]] = None
scala> f1.flatMap(x => f2.map(y => x + y))
res3: scala.concurrent.Future[Int] = scala.concurrent.impl.Promise$DefaultPromise#1c9dac55
scala> res3.value
res4: Option[scala.util.Try[Int]] = Some(Success(25))
Let's try something different:
scala> f1.flatMap(x => f2.map(y => x + y)).onComplete(println(_))
scala> Success(25) // Press enter here. onComplete mixed with REPL output.
Another try:
scala> val later=f1.flatMap(x => f2.map(y => x + y)); later.onComplete(println(_)); println(later.value);
None
later: scala.concurrent.Future[Int] = scala.concurrent.impl.Promise$DefaultPromise#7019b737
Success(25)
Now that was a confusing output. It's a good illustration of my point, though:
First, create later which contains a computation that will be
executed sometime in future.
Bind this computation with a event that will be triggered when it completes.
Print the result of the possibly unfinished computation.
Now think in two lines of execution. 1, 2 and 3 happens in the first, main line of execution, or the main thread. What lies inside the future, { 12 }, map, flatMat happens in another line of execution.
I'll steal RĂ©gis Jean-Gilles' excellent answer from https://stackoverflow.com/a/16358119/3328933
"Futures are executed on a dedicated thread pool. If your main program does not wait for the future, it will exit immediately and the future won't have a chance to execute. What you can do here is to use Await in your main program to block the main thread until the future executes:"
package reactive
import scala.concurrent.Await
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.future
import scala.concurrent.duration._
object futuresum extends App {
def f1 = future { 12 }
def f2 = future { 13 }
val result1 = f1.flatMap(x => f2.map(y => x + y))
result1 onSuccess { case res => println(res) }
Await.result(result1, 2 seconds)
val result2 = for (x <- f1; y <- f2) yield (x + y)
result2 onSuccess { case res => println(res) }
Await.result(result2, 2 seconds)
}
Use Await.result(result, duration) or Thread.sleep to wait future thread finish.
Then you can check future.isComplete to make sure that future is finished.
Await.result(result2, 3 seconds)
if(result2.isCompleted)
println(result2.value)

How to build processing in Scala depending on the multiple future Completion

I am new to Scala, and just start learning to use fully non-blocking nature of Scala API
I need to execute function after 2 tasks finish, curenly I do this following way:
import scala.concurrent._
import scala.util._
import scala.concurrent.duration._
val f1:Future[Int] = Future( {
Thread.sleep(800)
println(5)
5
})
val f2: Future[Int] = Future( {
Thread.sleep(500)
println(6)
6
})
val combined: Future[List[Int]] = Future.sequence(List(f1, f2))
combined.onComplete {
case Success(li) => li.foldLeft(0)((sum, x) => sum + x)
case Failure(t) => -1
}
Await.result(combined, 1 second)
Is there a more straight forward way to do this?
You can flatMap futures in for:
val f = for {
r1 <- f1
r2 <- f2
} yield (r1, r2) //any operations like `yield r1 + r2`
f.onSuccess { println }