![Page 1: Reactive Programming with Scala and Akka 読書会第1回](https://reader030.vdocuments.site/reader030/viewer/2022020213/58a9ccf71a28ab403d8b58a9/html5/thumbnails/1.jpg)
Reactive Programmingwith Scala and Akka読書会 第1回
2016-05-27 はせがわ
http://goo.gl/KOBfQM
![Page 2: Reactive Programming with Scala and Akka 読書会第1回](https://reader030.vdocuments.site/reader030/viewer/2022020213/58a9ccf71a28ab403d8b58a9/html5/thumbnails/2.jpg)
Chapter2Functional Reactive Programming in Scala
• 不変性(immutability)• 遅延評価(lazy evaluation)• 関数合成(Function Composition)• Functor, Applicative Functor and Monad
この章で学ぶこと
![Page 3: Reactive Programming with Scala and Akka 読書会第1回](https://reader030.vdocuments.site/reader030/viewer/2022020213/58a9ccf71a28ab403d8b58a9/html5/thumbnails/3.jpg)
Mutability(可変性) vs Immutability(不変性)
Functional Reactive なプログラミングを行うには
Immutable(不変)の方が良い
![Page 4: Reactive Programming with Scala and Akka 読書会第1回](https://reader030.vdocuments.site/reader030/viewer/2022020213/58a9ccf71a28ab403d8b58a9/html5/thumbnails/4.jpg)
Immutable(不変)が良い理由
• 参照透過性(Referential Transparency)が保ちやすい
• 状態(State)が複数スレッドで共有されても問題ない
• ロックが不要
![Page 5: Reactive Programming with Scala and Akka 読書会第1回](https://reader030.vdocuments.site/reader030/viewer/2022020213/58a9ccf71a28ab403d8b58a9/html5/thumbnails/5.jpg)
Collections(Immutable)• Immutable(不変)なものとMutable(可変)なものがある※ ここではImmutableなものを見ていく
• 要素の追加や削除は新たなインスタンスをつくる
• メソッド:map, flatMap, filter, sort, fold...
![Page 6: Reactive Programming with Scala and Akka 読書会第1回](https://reader030.vdocuments.site/reader030/viewer/2022020213/58a9ccf71a28ab403d8b58a9/html5/thumbnails/6.jpg)
Lazy Evaluation(遅延評価)
• 「lazy val 変数名 = 値や式」
• 「lazy」をつけると変数は宣言時には評価されていない
• 処理中に初めてアクセスされた際に評価される
• 評価された変数は再評価されない
![Page 7: Reactive Programming with Scala and Akka 読書会第1回](https://reader030.vdocuments.site/reader030/viewer/2022020213/58a9ccf71a28ab403d8b58a9/html5/thumbnails/7.jpg)
Function Composition(関数合成)• 関数オブジェクトの(B => C)と(A =>B)がある場合、関数合成で(A => C)ができる
• 関数オブジェクト 例:(B => C)• B型のオブジェクトの引数をとってC型のオブジェクトを返す関数オブジェクト
![Page 8: Reactive Programming with Scala and Akka 読書会第1回](https://reader030.vdocuments.site/reader030/viewer/2022020213/58a9ccf71a28ab403d8b58a9/html5/thumbnails/8.jpg)
カリー化(Curry)• 関数func(A, B):Cがある場合、カリー化することでfunc(A):(B => C)にすることができる
• Scalaだと関数にcurriedキーワードを使うことでカリー化できる
scala> def add(a: Int, b:Int): Int = { a + b }add: (a: Int, b: Int)Int
scala> val curriedAdd = (add _).curriedcurriedAdd: Int => (Int => Int) = <function1>
![Page 9: Reactive Programming with Scala and Akka 読書会第1回](https://reader030.vdocuments.site/reader030/viewer/2022020213/58a9ccf71a28ab403d8b58a9/html5/thumbnails/9.jpg)
Functor, Applicative Functor, and Monads• 関数型プログラミングの重要なコンセプト
• Scalaz=純粋関数型PGを行うためのライブラリ
• ざっくりと Functor ◁- Applicative ◁-Monad と継承している
![Page 10: Reactive Programming with Scala and Akka 読書会第1回](https://reader030.vdocuments.site/reader030/viewer/2022020213/58a9ccf71a28ab403d8b58a9/html5/thumbnails/10.jpg)
型クラス(Typeclass)• ふるまいを定義するインタフェース
• ふるまいとデータを定義するデザパタ• mapだと中身を変えるふるまい(関数)に対しListやOptionなどのデータに適用できる
• Functor、Applicative、Monadは型クラス
![Page 11: Reactive Programming with Scala and Akka 読書会第1回](https://reader030.vdocuments.site/reader030/viewer/2022020213/58a9ccf71a28ab403d8b58a9/html5/thumbnails/11.jpg)
Functor• map関数• map[A, B](fa: F[A])(f: A=>B): F[B]
• 関数を受け取りFunctor(※ここだとF)が持つ値を変換する
• 例えばFをListやOptionだとしてAがIntでBがString(Bは同じ型(Int)でも可)
![Page 12: Reactive Programming with Scala and Akka 読書会第1回](https://reader030.vdocuments.site/reader030/viewer/2022020213/58a9ccf71a28ab403d8b58a9/html5/thumbnails/12.jpg)
Applicative Functor part.1
• Functorを拡張したもの
• pure関数• pure[A](a: => A): F[A] = point(a)
• point[A](a: => A): F[A]• point関数はpure関数の別名
• pureはfinalでpointはoverride可能にすることで色んな型に対応できる
@grimroseさんの助言
![Page 13: Reactive Programming with Scala and Akka 読書会第1回](https://reader030.vdocuments.site/reader030/viewer/2022020213/58a9ccf71a28ab403d8b58a9/html5/thumbnails/13.jpg)
Applicative Functor part.2
• ap関数• ap[A, B](fa: =>F[A])(f: =>F[A=>B]):F[B]• Apply[F]の関数
• <*>関数• <*>[B](f: F[A=>B]):F[B]
![Page 14: Reactive Programming with Scala and Akka 読書会第1回](https://reader030.vdocuments.site/reader030/viewer/2022020213/58a9ccf71a28ab403d8b58a9/html5/thumbnails/14.jpg)
Monad• Applicative Functorを拡張したもの
• bind関数• bind[A, B](fa: F[A])(f: A=>F[B]):F[B]• 実質的にはflatMap