Last active
December 13, 2015 17:58
-
-
Save mather/4951569 to your computer and use it in GitHub Desktop.
Futureを使ってみる。 Scala 2.10.0
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を使うときにExecutionContextが必要。 */ | |
implicit val execctx = ExecutionContext.global | |
/* futureはscala.concurrentパッケージに定義されているシンタックスシュガー */ | |
val a = future { Thread sleep 100000 ; "hoge" } | |
/* 次のFutureの処理を合成 */ | |
val b = a flatMap ( (str:String) => future { "fuga" + str } ) | |
/* 成功したら結果を出力 */ | |
b.onSuccess{case a => println(a)} | |
//=> "fugahoge" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment