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)