c******o 发帖数: 1277 | 1 运行结果在下一贴
import scala.concurrent._
import ExecutionContext.Implicits.global
object F extends App {
val xFut: Future[Int] = future {
Thread.sleep(10000);
println("x happened");
10
}.flatMap(i => Future.successful(i + 1))
val aFut: Int => Future[(Int, Int)] = a => {
val yFut: Future[Int] = future {
println("y begin")
Thread.sleep(6000);
println("y happened " + a);
20
}
val zFut: Future[Int] = future {
println("z begin")
Thread.sleep(5000);
println("z hapenned " + a);
30
}
for {
y <- yFut
z <- zFut
} yield ((y,z))
}
val result: Future[(Int, Int, Int)] = for {
x <- xFut
(y,z) <- aFut(x)
} yield (x, y, z)
Thread.sleep(20000)
println("nThe end")
} |
c******o 发帖数: 1277 | 2 运行结果
x happened
y begin
z begin
z hapenned 11
y happened 11
The end |
m*******l 发帖数: 12782 | 3 yield时不时从C#抄的
【在 c******o 的大作中提到】 : 运行结果 : x happened : y begin : z begin : z hapenned 11 : y happened 11 : The end
|
p*****2 发帖数: 21240 | 4 (defn -main []
(let [c1 (chan) c2 (chan) c3 (chan) c4 (chan) c5 (chan) c6 (chan)]
(go
(
(println "x happened")
(>! c1 10))
(go
(let [a (inc (
(>! c2 a)
(>! c2 a)))
(go
(let [a (
(go
(println "y begin")
(
(println "y happened " a)
(>! c3 20))
(go
(println "z begin")
(
(println "z happened " a)
(>! c4 30))))
(go
(>! c5 (list (
(go
(>! c6 (cons (
(println (
(println "The end")))
x happened
y begin
z begin
z happened 11
y happened 11
(11 20 30)
The end |
z****e 发帖数: 54598 | 5 我相信大多数人一眼扫过去,印象最深刻的是这个
))))))
【在 p*****2 的大作中提到】 : (defn -main [] : (let [c1 (chan) c2 (chan) c3 (chan) c4 (chan) c5 (chan) c6 (chan)] : (go : (: (println "x happened") : (>! c1 10)) : : (go : (let [a (inc (: (>! c2 a)
|
p*****2 发帖数: 21240 | 6
难道不是go go go吗?
【在 z****e 的大作中提到】 : 我相信大多数人一眼扫过去,印象最深刻的是这个 : ))))))
|