我有以下代码:
package functorapplication
import scalaz._
import Scalaz._
import scalaz.concurrent.Future
object FunctorApplication2 extends App {
val f1 = Future(3)//(ec)
val f2 = Future(4)//(ec)
val f3 = Future(5)//(ec)
val calculate = (a: Int) => (b: Int) => (c: Int) => a + b + c
val area = f1 <*> (f2 <*> (f3 <*> Future(calculate)))//(ec))) // Future(12)
//println(area)//BindSuspend(scalaz.concurrent.Future$$Lambda...
println("starting")
val summed = for {
a <- area
} yield {
println(a)
}
area.map(value => println(value))
//println(summed)//Suspend(scalaz.concurrent.Future$$Lambda...
println("done")
}
这给出了以下结果:
starting
done
关键在于 - 未来似乎没有价值在理解或被映射的价值。
我的问题是:如何在Scalaz中获得未来的价值?
笔记:
这是我的scala版本
scalaVersion := "2.12.5",
这是我的scalaz版本
"org.scalaz" %% "scalaz-core" % "7.2.26",
"org.scalaz" %% "scalaz-concurrent" % "7.2.26",
"org.scalaz" %% "scalaz-effect" % "7.2.26",
"org.scalaz" %% "scalaz-iteratee" % "7.2.26"
虽然我对scalaz Future知之甚少,但我认为你只是错过了.start
或.unsafeStart
才真正开始运行Future
。
即使你使用.start
,你也需要使用像scala.io.StdIn.readLine("press enter to exit")
这样的东西来阻止主线程。
另一种方法是使用.run
或unsafePerformSync
来阻止并等待结果。
val res = summed.run
println(res) // currently Unit because it yields println
println("done")
这将产生副作用并运行Future并返回结果。
我通过查看scalaz.concurrent.Future
的源代码找到了这一切。
您应该注意,在实践中,由于错误处理,人们使用Task
,除非您正在编写库(如源代码中所述)。
如果您持有Future[A]
类型的值,那么您有资格在将来的某个时间获得
A
类型的值Throwable
类型的值。因此,未来代表潜在的未来价值。获得未来价值的唯一已知方法是等待它。
import scala.concurrent._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
val f1 = Future(3)//(ec)
val f2 = Future(4)//(ec)
val f3 = Future(5)//(ec)
val calculate = (a: Int) => (b: Int) => (c: Int) => a + b + c
val area: Future[Int] = for {
v1 <- f1
v2 <- f2
v3 <-f3
} yield calculate(v1)(v2)(v3)
println("starting")
println("adding future side effect")
//this happens in the future
val withSideEffect = area.map(value => println("side-effect: " + value))
println("awaiting now")
//now, let's wait
println("Await: " + Await.ready(withSideEffect, Duration.Inf).value)
println("done")
这打印:
starting
adding future side effect
awaiting now
side-effect: 12
Await: Some(Success(()))
done