7.
Scalaz
• https://github.com/scalaz/scalaz
• キャッチフレーズ
• 昔: Scalaz: Type Classes and Pure Functional Data
Structures for Scala
• 今: An extension to the core Scala library for functional
programming. http://typelevel.org
• 最新の関数型プログラミングを可能にする機能群を
Scala向けに⽤用意
• 型クラス
• 純粋関数型データ構造
• Haskellで実績のある機能群をScalaで実現
8.
Pipelineプログラミング(1)
def pipeline(x: Int) = h(g(f(x))
def pipeline(x: Option[Int]) = x.map(f).map(g).map(h)
def pipeline(x: Int) = x |> f |> g |> h
関数呼び出し
Functor
Monad
def pipeline(x: Option[Int]) = x.flatMap(f).flatMap(g).flatMap(h)
def pipeline(x: Option[Int]) =
for (a <- f(x); b <- g(a); c <- h(b)) yiled c
9.
Pipelineプログラミング(2)
val pipeline =
State(f).flatMap(x => State(g(x))).flatMap(x => State(h(x)))
pipeline.run(10)
val pipeline =
for (a <- State(f);b <- State(g(a)); c <- State(h(c))) yield c
pipeline.run(10)
Monad (インタープリター型)
参考: [FP] パイプライン・プログラミング
http://modegramming.blogspot.jp/2015/06/fp.html
val pipeline = io.linesR("infile.txt").filter(_.nonEmpty).map(x =>
s"${x.length}:$x”)
pipeline.run.run
scalaz-stream (Process Monad)
10.
Stateモナドによるパイプライン
State Monad
FuncionState Function Function
State
Value
Value Value Value
Value Value
11.
Task
• scalaz.concurrent.Task
• TryとFutureを合わせた機能を持つモナド
• ⻑⾧長所
• Scalazとの親和性
• 利利⽤用シーンに合わせて並列列処理理の使⽤用・⾮非使⽤用を選択でき
る
• Futureのようにスレッドを⼤大量量消費しない
• 純粋関数型指向のインタフェース(副作⽤用はrunで実⾏行行)
• 関数型的な合成可能な部品化
val t: Task[Int] = Task(f(1000))
val i: Int = t.run
val i: Int = Task(f(1000)).run
14.
Reactiveの実現
Service Cluster
CPU Core
Function
Service
Message
Message
Message
Function
Function
Function
Function
Function
Service
Service
Service
Service
Function
Reactive
Concurrent
Parallel
Distributed
Responsive
Resillience
Elasticity
Message Driven
Concurrent
Parallel
Distributed
Be the first to comment