Scala の Future と Haskell の IO

83fb9f537f0aedcfbfc22dc395e85c84?s=47 daimatz
May 11, 2014

Scala の Future と Haskell の IO

83fb9f537f0aedcfbfc22dc395e85c84?s=128

daimatz

May 11, 2014
Tweet

Transcript

  1. Scala ͷ Future
 ͱ
 Haskell ͷ IO 2014-05-11 (Sun) ؔ਺ܕLTେձ

    @daimatz
  2. ୭ • @daimatz • Dennoo, Inc. • Scala ͰΞυαʔόΛॻ͍͍ͯ·͢ •

    Haskell ྺ 2೥ɺ Scala ྺ ൒೥͘Β͍
  3. Scala ͷ Future ͱ Haskell ͷ IO ͸ࣅ͍ͯΔ ݴ͍͍ͨ͜ͱ͸͜Ε͚ͩ

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

    ΛݺͿͱඇಉظͰܭࢉ͕૸Δ • ίʔϧόοΫؔ਺Λొ࿥͢Δ͜ͱͰͦͷܭࢉ͕ऴΘͬ ͨ͋ͱͷڍಈΛ੍ޚ͢Δ
  5. 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 ඪ४൛Ͱ΋໊લ͸ҧ͏͕
 େମಉ͡
  6. Scala Ͱ࢖͏ओͳ Future ࣮૷ • com.twitter.util.Future" • Twitter ʹΑΔ࣮૷ɺ Finagle

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

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

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


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

    • Await.result(x: Future[A]): A" • εϨουͷ join Έ͍ͨͳ΋ͷ͕ͩɺεϨουΛϑϧ׆༻͢Δ ϓϩάϥϜͰ͸࢖ͬͯ͸͍͚ͳ͍ • IO ΛऔΓআ͘ېஅͷຐ๏ unsafePerformIO • unsafePerformIO :: IO a -> a
  11. ࣅ͍ͯͳ͍ͱ͜Ζɿྫ֎ͷѻ͍ • 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] ͸ྫ֎Λϥοϓ
  12. ·ͱΊ • Scala ͷ Future ͱ Haskell ͷ IO ͸ࣅ͍ͯΔ

    • ๻͸ͦΕʹؾ͍ͮͨޙ͢ΜͳΓͱ Future ΛཧղͰ͖ͨ͠ɺϓϩά ϥϜ΋ॻ͚ΔΑ͏ʹͳͬͨ • Haskell Λ஌͍ͬͯΔਓ͕ Scala ͷ Future Λ৮Γ࢝Ίͨͱ͖ʹ ஌͍ͬͯΔͱศར • Scala ެࣜͷઆ໌͸Θ͔Γʹ͍͘ͱࢥ͏…
 http://docs.scala-lang.org/ja/overviews/core/futures.html • twitter/util ͱ twitter/finagle Λಡ΋͏
  13. ʢ௥هʣ • ཁ͸ʮFuture ͸Ұํ޲ੑϞφυͳͷͰɺʢ؆୯ʹ͸ʣ ໭Εͳ͍͔Β࢖͍ํʹ஫ҙʯͱݴ͍ͬͯΔ͚ͩͰ͢ • Ұํ޲ੑͷϞφυ͸ Haskell Ͱ͸ IO

    ͕Ұ൪Θ͔Γ΍ ͍͢ྫͳͷͰɺͦΕͱͷରൺΛڍ͛·ͨ͠