Skip to content

Instantly share code, notes, and snippets.

@mather
Last active December 13, 2015 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mather/4951569 to your computer and use it in GitHub Desktop.
Save mather/4951569 to your computer and use it in GitHub Desktop.
Futureを使ってみる。 Scala 2.10.0
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