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

Scala ZIOをバッチ処理に使ってみた

Scala ZIOをバッチ処理に使ってみた

More Decks by リチャード 伊真岡

Other Decks in Technology

Transcript

  1. 関数型の最大のメリット(の一つ)はtestability! ①
 “Functional programming ordinarily gives us the incredible ability

    to easily test our software.” Beautiful, Simple, Testable Functional Effects for Scala JOHN A DE GOES (blog) Feb, 2019 John De Goes氏 ZIO作者
  2. 関数型の最大のメリット(の一つ)はtestability! ②
 “I would argue that the whole point of

    functional programming in general is testing” Free as in Monads - ETE 2017 Daniel Spiewak氏
  3. descriptionとinterpreterの分離
 val programDescription = for { … <- queryDatabase(…) …

    <- callHttpService(…) … <- doSomeMagic(…) } yield finalResult def main(args: Array[String]): Unit = { interpreter.unsafeRun(programDescription) }
  4. descriptionとinterpreterの分離
 val programDescription = for { … <- queryDatabase(…) …

    <- callHttpService(…) … <- doSomeMagic(…) } yield finalResult def main(args: Array[String]): Unit = { interpreter.unsafeRun(programDescription) }
  5. descriptionとinterpreterの分離
 val programDescription = for { … <- queryDatabase(…) …

    <- callHttpService(…) … <- doSomeMagic(…) } yield finalResult def main(args: Array[String]): Unit = { interpreter.unsafeRun(programDescription) } tree構造
 副作用

  6. JOHN A DE GOES - The False Hope of Managing

    Effects with Tagless-Final in Scala 

  7. ZIOの戻り値型
 ZIO[R, E, A] • R - Environment Type. •

    E - Failure Type. • A - Success Type. <- Exception, Error
  8. ZIOの戻り値型
 ZIO[R, E, A] • R - Environment Type. •

    E - Failure Type. • A - Success Type. <- DI components, DB, Config, ThreadPool
  9. ZIOの戻り値型
 ZIO[R, E, A] • R - Environment Type. •

    E - Failure Type. • A - Success Type. <- DI components, DB, Config, ThreadPool
  10. trait S3Service { def getFromS3(...): ZIO[Any, Throwable, S3Result] } S3

    trait S3Component { val service: S3Service } object S3ProductionComponent extends S3Component { val service: S3.Service = ... }
  11. trait S3Service { def getFromS3(...): ZIO[Any, Throwable, S3Result] } S3

    trait S3Component { val service: S3Service } object S3ProductionComponent extends S3Component { val service: S3.Service = ... } object S3TestComponent extends S3Component { val service: S3.Service = ... }
  12. trait MainComponent extends S3Component with AthenaComponent with DatabaseComponent with ConfigComponent

    with LoggerComponent with … … cake pattern
 
 どっちかというとおまんじゅうに見える 

  13. CirquaでのZIO利用例
 def run(args: List[String]) = { _ <- logger.info(...) _

    <- logger.info(...extra info...) _ <- databaseComponent.setup(...) result <- athenaComponent.execute(...) ... } yield ()
  14. CirquaでのZIO利用例
 def run(args: List[String]): ZIO[S3Component with AthenaComponent with …, Throwable,

    BatchResult] = { _ <- logger.info(...) _ <- logger.info(...extra info...) _ <- databaseComponent.setup(...) _ <- athenaComponent.setUp(...) ... } yield () あえてZIOの型を書いてみる(必要ない場合普通は書かない)

  15. CirquaでのZIO利用例
 def run(args: List[String]): ZIO[S3Component with AthenaComponent with …, Throwable,

    BatchResult] = { _ <- logger.info(...) _ <- logger.info(...extra info...) _ <- databaseComponent.setup(...) _ <- athenaComponent.setUp(...) ... } yield () def setUp(...): ZIO[..., …, ...] = for { _ <- s3Component.setupSomething() … }
  16. 関数型の最大のメリット(の一つ)はtestability! ②
 “I would argue that the whole point of

    functional programming in general is testing” Free as in Monads - ETE 2017 Daniel Spiewak氏 関数型プログラミング等で特に著名 これ、Algebraic Lawsの話をしている。 (リチャード)
  17. John De Goes氏の別の発言
 “Tagless-final type classes do not, in general,

    have algebraic laws. Most have no laws at all. This represents a serious abuse of the construct of a type class ...” Beautiful, Simple, Testable Functional Effects for Scala JOHN A DE GOES (blog) Feb, 2019 John De Goes氏 ZIO作者