developers summit 2015 - scala monad
TRANSCRIPT
Scala {Monad}
Who Am I• Now: Dynalyst • Lang: Korean(30+ years) Japanese(8+ years) JAVA(8.2+ years) Scala(a year)
• AdTech: AMoAd DSP CAA Reward
What Dynalyst• Dynamic Retargeting + Analyst
•ユーザーの趣味嗜好に適した広告配信を行う国内初のスマートフォンに特化したダイナミックリターゲティング広告
Scala USAGE
Today’s Talk
•Monads in Scala Standard Library
•How to use them
Monad?
•Effects on a value •Structure that represents computations defined as sequences of steps
Monad?
•Effects on a value:unit
•Structure that represents computations defined as sequences of steps:flatMap
Effects on a value• VALUE • MONAD(VALUE) • Option(VALUE),List(VALUE),Try(VALUE),Future(VALUE)
Sequencial Computations
• X -> Y -> Z • M[X] -> M[Y] -> M[Z]
f g
f’ g’
Behaviour• f = X => M[Y]g = Y => M[Z]
• X -> M[X] -> M[Y]
• M[Y] -> M[Z] -> …
unit flatMap
flatMap flatMap
Simple User DAO
Option
Effects on a
Nullable Value
(Some or None)
Unit
FlatMap
Effects on a Nullable Value
Some(User) or None
Exception Safety?
Try
Effects on a
Throwable Value
(Success or Failure)
Unit
FlatMap
Effects on a Nullable & Throwable Value
Success(Some(User)) orSuccess(None) or
Failure(throwable)
Blocking Thread?
Future
Effects on a
Blocking Value
(Success or Failure)
Unit
FlatMap
Effects on a Nullable & Throwable Valuein Asynchronously
Success(Some(User)) orSuccess(None) or
Failure(throwable)
List
Effects on
Iterable Values
(List or Nil)
Monad Laws
• Left Identity • Right Identity • Associativity
Pull a value out of Monad
•Helper Functions:getOrElse,fold,foreach,recover…
•Pattern Match •3rd Party Library:Akka,Spray…
Akka
Spray
Benefits
• Keep your codes CLEAN
• Easy To Guess(READABLE)
• MAINTAINABLE
• Less Code Less BUGS