Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Scala の Future と Haskell の IO

daimatz
May 11, 2014

Scala の Future と Haskell の IO

daimatz

May 11, 2014
Tweet

More Decks by daimatz

Other Decks in Technology

Transcript

  1. Future ͱ͸ • ඇಉظܭࢉͷϞσϧ • ֓೦ࣗମ͸ Promise ͱ΋͍͏ • Future

    ΛݺͿͱඇಉظͰܭࢉ͕૸Δ • ίʔϧόοΫؔ਺Λొ࿥͢Δ͜ͱͰͦͷܭࢉ͕ऴΘͬ ͨ͋ͱͷڍಈΛ੍ޚ͢Δ
  2. Future ྫ val f: Future[List[String]] =
 session.getRecentPosts" ! f.respond {"

    case Return(posts) =>" for (post <- posts) println(post)" case Throw(throwable) =>" println(“error:” + throwable.getMessage)" } • Twitter ൛ͷྫɻ Scala ඪ४൛Ͱ΋໊લ͸ҧ͏͕
 େମಉ͡
  3. Scala Ͱ࢖͏ओͳ Future ࣮૷ • com.twitter.util.Future" • Twitter ʹΑΔ࣮૷ɺ Finagle

    ͱ͔ • scala.concurrent.Future" • Scala 2.10 Ͱඪ४ʹೖͬͨɺ Akka ͱ͔ • java.util.concurrent.Future" • Java ඪ४ͷ࣮૷ɺ Netty ͱ͔
  4. Scala ͷ Future ͱ Haskell ͷ IO ͸ࣅ͍ͯΔ • Scala

    ͷ Future ͸ɺ Haskell ͷ IO ͱಉ͡Α͏ͳ
 ײ֮Ͱ࢖͑Δɻݟͨ໨΋Α͘ࣅ͍ͯΔɻ • ܕͰΘ͔Δ • ϞφυͰ͋Δ • ېஅͷຐ๏͕͋Δ
  5. ܕͰΘ͔Δ • def foo(x: String): Future[Int]" • ඇಉظܭࢉ͕ߦΘΕΔ͜ͱΛ Future[A] Ͱද͢

    • bar :: String -> IO Int" • ෭࡞༻ͷ͋ΔܭࢉͰ͋Δ͜ͱΛ IO a Ͱද͢
  6. ϞφυͰ͋Δ • for {
 a <- someFutureFunction()
 b <- otherFutureFunction(a)


    } yield b" • do
 a <- someIOFunction
 b <- otherIOFunction a
 return b
  7. ېஅͷຐ๏͕͋Δ • Future ΍ IO ͷੈքʹҰ౓ೖͬͨΒجຊతʹ͸ೋ౓ͱ໭Εͳ͍ • Future ΛऔΓআ͘ېஅͷຐ๏ Await.result

    • Await.result(x: Future[A]): A" • εϨουͷ join Έ͍ͨͳ΋ͷ͕ͩɺεϨουΛϑϧ׆༻͢Δ ϓϩάϥϜͰ͸࢖ͬͯ͸͍͚ͳ͍ • IO ΛऔΓআ͘ېஅͷຐ๏ unsafePerformIO • unsafePerformIO :: IO a -> a
  8. ࣅ͍ͯͳ͍ͱ͜Ζɿྫ֎ͷѻ͍ • com.twitter.util.Try[A]" • ྫ֎͕ى͜Δ͔΋͠Εͳ͍ܭࢉΛϥοϓ͢ΔΫϥε • ͜Ε΋ Scala 2.10 Ͱ

    scala.util.Try ͱͯ͠ඪ४ʹ • Future[A] ͷ࣮ମ͸ Promise[A] Ͱɺܭࢉ͕׬ྃͨ͠Β Try[A] ʹͳ͍ͬͯΔ • Try[A] ͷ࣮ମ͸ Return[A] ΋͘͠͸ Throw[A] • Return[A] ͸݁ՌΛϥοϓɺ Throw[A] ͸ྫ֎Λϥοϓ
  9. ·ͱΊ • Scala ͷ Future ͱ Haskell ͷ IO ͸ࣅ͍ͯΔ

    • ๻͸ͦΕʹؾ͍ͮͨޙ͢ΜͳΓͱ Future ΛཧղͰ͖ͨ͠ɺϓϩά ϥϜ΋ॻ͚ΔΑ͏ʹͳͬͨ • Haskell Λ஌͍ͬͯΔਓ͕ Scala ͷ Future Λ৮Γ࢝Ίͨͱ͖ʹ ஌͍ͬͯΔͱศར • Scala ެࣜͷઆ໌͸Θ͔Γʹ͍͘ͱࢥ͏…
 http://docs.scala-lang.org/ja/overviews/core/futures.html • twitter/util ͱ twitter/finagle Λಡ΋͏