Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Scala ZIOをバッチ処理に使ってみた
Search
リチャード 伊真岡
July 29, 2019
Technology
1
900
Scala ZIOをバッチ処理に使ってみた
リチャード 伊真岡
July 29, 2019
Tweet
Share
More Decks by リチャード 伊真岡
See All by リチャード 伊真岡
データマネジメント知識体系ガイドとともに学ぶデータモデリング
richardimaokajp
0
310
Adobe After EffectsによるExplainer Video作成
richardimaokajp
0
110
Java 5.0時代の非同期処理技術から学び直すScala/Java非同期処理
richardimaokajp
9
6.6k
非同期処理の歴史から見たコンピューティングの進化
richardimaokajp
12
6.8k
出張Akkaワークショップ
richardimaokajp
0
2.1k
Akkaによるスレッドの使い方
richardimaokajp
4
1.4k
Other Decks in Technology
See All in Technology
明日からできる!技術的負債の返済を加速するための実践ガイド~『ホットペッパービューティー』の事例をもとに~
recruitengineers
PRO
3
400
AndroidデバイスにFTPサーバを建立する
e10dokup
0
250
ユーザーストーリーマッピングから始めるアジャイルチームと並走するQA / Starting QA with User Story Mapping
katawara
0
210
30分でわかる『アジャイルデータモデリング』
hanon52_
9
2.7k
ビジネスモデリング道場 目的と背景
masuda220
PRO
9
530
OpenID Connect for Identity Assurance の概要と翻訳版のご紹介 / 20250219-BizDay17-OIDC4IDA-Intro
oidfj
0
280
Culture Deck
optfit
0
420
管理者しか知らないOutlookの裏側のAIを覗く#AzureTravelers
hirotomotaguchi
2
430
データ資産をシームレスに伝達するためのイベント駆動型アーキテクチャ
kakehashi
PRO
2
540
Cloud Spanner 導入で実現した快適な開発と運用について
colopl
1
680
現場で役立つAPIデザイン
nagix
33
12k
Platform Engineeringは自由のめまい
nwiizo
4
2.1k
Featured
See All Featured
StorybookのUI Testing Handbookを読んだ
zakiyama
28
5.5k
Typedesign – Prime Four
hannesfritz
40
2.5k
Building a Scalable Design System with Sketch
lauravandoore
461
33k
How to Ace a Technical Interview
jacobian
276
23k
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.6k
The Pragmatic Product Professional
lauravandoore
32
6.4k
Embracing the Ebb and Flow
colly
84
4.6k
Building Applications with DynamoDB
mza
93
6.2k
Gamification - CAS2011
davidbonilla
80
5.1k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.7k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Speed Design
sergeychernyshev
27
790
Transcript
Scala ZIOをバッチ処理で使ってみた リチャード 伊真岡 マーベリック株式会社
関数型の最大のメリット(の一つ)は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作者
関数型の最大のメリット(の一つ)はtestability! ② “I would argue that the whole point of
functional programming in general is testing” Free as in Monads - ETE 2017 Daniel Spiewak氏
純粋関数はtestabilityが高い input 1 input 2 output
純粋関数はtestabilityが高い test input 1 test input 2 output expected output
assert!
副作用はテストが難しい Database File HTTP
副作用を含むコード 副作用 純粋関数呼び出し
テストしづらい... そこで関数型のテクニックを使う!
descriptionとinterpreterの分離 val programDescription = for { … <- queryDatabase(…) …
<- callHttpService(…) … <- doSomeMagic(…) } yield finalResult def main(args: Array[String]): Unit = { interpreter.unsafeRun(programDescription) }
descriptionとinterpreterの分離 val programDescription = for { … <- queryDatabase(…) …
<- callHttpService(…) … <- doSomeMagic(…) } yield finalResult def main(args: Array[String]): Unit = { interpreter.unsafeRun(programDescription) }
descriptionとinterpreterの分離 val programDescription = for { … <- queryDatabase(…) …
<- callHttpService(…) … <- doSomeMagic(…) } yield finalResult def main(args: Array[String]): Unit = { interpreter.unsafeRun(programDescription) } tree構造 副作用
descriptionとinterpreterの分離 • この流れに沿うテクニックとしてfree-monad, tagless-finalなどがある • しかし...
John De Goes氏のThe Death of Tagless Finalより https://skillsmatter.com/skillscasts/13247-scala-matters
John De Goes氏のThe Death of Tagless Finalより https://skillsmatter.com/skillscasts/13247-scala-matters
副作用同士はtestで比較できない input 1 input 2 description expected description assert!??? e.g.
println
JOHN A DE GOES - The False Hope of Managing
Effects with Tagless-Final in Scala
Scala ZIOの方がtagless finalよりイイ? • testabilityの向上 • とくに非関数型プログラマになじみやすい(らしい) ◦ -> ZIO自体の開発動機のひとつ
(ZIO作者談)
Scala ZIOをバッチ処理で使ってみた
ZIOの戻り値型 ZIO[R, E, A] • R - Environment Type. •
E - Failure Type. • A - Success Type.
ZIOの戻り値型 ZIO[R, E, A] • R - Environment Type. •
E - Failure Type. • A - Success Type. <- Exception, Error
ZIOの戻り値型 ZIO[R, E, A] • R - Environment Type. •
E - Failure Type. • A - Success Type. <- DI components, DB, Config, ThreadPool
None
これがCirquaだ!
これがCirquaだ!
これがCirquaだ!
これがCirquaだ!
これがCirquaだ! 独立している!!
ZIOの戻り値型 ZIO[R, E, A] • R - Environment Type. •
E - Failure Type. • A - Success Type. <- DI components, DB, Config, ThreadPool
ZIOでのDI手順例: DIするコンポーネントを特定 Config S3 Logger Athena Database etc
trait S3Service { def getFromS3(...): ZIO[Any, Throwable, S3Result] } S3
例: cake pattern
trait S3Service { def getFromS3(...): ZIO[Any, Throwable, S3Result] } S3
trait S3Component { val service: S3Service }
trait S3Service { def getFromS3(...): ZIO[Any, Throwable, S3Result] } S3
trait S3Component { val service: S3Service } object S3ProductionComponent extends S3Component { val service: S3.Service = ... }
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 = ... }
trait MainComponent extends S3Component with AthenaComponent with DatabaseComponent with ConfigComponent
with LoggerComponent with … … cake pattern どっちかというとおまんじゅうに見える
\アッタカイヨー/
CirquaでのZIO利用例 def run(args: List[String]) = { _ <- logger.info(...) _
<- logger.info(...extra info...) _ <- databaseComponent.setup(...) result <- athenaComponent.execute(...) ... } yield ()
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の型を書いてみる(必要ない場合普通は書かない)
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() … }
cakeってアンチパターンじゃないの?
分割された一つ一つのfor文は 小さいcakeになるので、昔ながらの cake patternの問題を避けられる ZIO cake (module pattern) (らしいよ)
ZIOとテスト • Effect(副作用)はだいたいDIのコンポーネントに集中する • DIを使ってテスト!...あれ?
関数型の最大のメリット(の一つ)は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の話をしている。 (リチャード)
Algebraic Lawsの話 • プログラムが満たすべきルールを、型クラスが満たすべきルールとして定義してい る(はず…間違ってる?) • そもそもデータベース副作用、ファイル副作用に満たすべきAlgebraic Lawつまり代 数的規則などない、数学的に厳密に規則を議論できない •
それはtagless finalに代えてZIOを導入しても同じ
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作者
ZIOとテスト • イマイチdescriptionとinterpreterの分離によるtestabilityの向上がわからない • DIを上手く使えばテストが書けるが、それはdescriptionとinterpreterの分離による testability向上ではないのでは? • 結局この点はについてはtagless-finalも同じだったし、ZIOでも副作用のtestability が向上してるわけではなさそうに見えるのだが… •
というわけでdescriptionとinterpreterの分離の流れに沿うZIOもtestabilityの向上と いう面で見るとメリットが「私には」理解できなかった • モックを上手く使えばテストが書けるが(同上)
ZIOは理解しやすくtestability以外の メリットも有る、という話なら分かる
ZIOの他のメリット!!
省略 ごめん、まとめる時間がなかった