Slide 1

Slide 1 text

Partially applied Functional Programming in Scala Jun Tomioka (M3, inc.) Scalaでの部分的な関数型プログラミング

Slide 2

Slide 2 text

M3, Inc. Jun Tomioka Twitter: @jooohn1234 Github: jooohn Love: Our very first baby Had great paternity leave! エムスリー株式会社の富岡純です。好きなものは第一子です。育休を 取得していました。

Slide 3

Slide 3 text

エムスリー株式会社は、インターネットというメディアを活用し、医療の 世界を変革します。

Slide 4

Slide 4 text

What is “Functional Programming”? 関数型プログラミングとは?

Slide 5

Slide 5 text

What is “Functional Programming”? ● “We construct our programs using only pure functions” ○ Functions without side effects ● Benefits: ○ Increase modularity ○ Easier to test, reuse, parallelize, generalize, and reason about 「純粋関数だけでプログラムを組み立てる」ことを前提とすることで、テ ストしやすいなどの様々な恩恵を受けられます。

Slide 6

Slide 6 text

we construct our programs using ONLY PURE FUNCTIONS 「純粋関数だけでプログラムを組み立てる」

Slide 7

Slide 7 text

ONLY PURE FUNCTIONS 「純粋関数だけ」

Slide 8

Slide 8 text

Using ONLY pure functions is powerful, but... ● Should we: ○ give up existing awesome but impure libraries? ○ re-implement entire project? ● Can’t ignore its learning cost 「だけ」というのは強力ですが、非純粋な依存ライブラリをどうするか、 学習コストが馬鹿にならない等の問題があります。

Slide 9

Slide 9 text

There are 2 types of FP techniques ● For a pure program ○ val, immutable, ... ● For a effectful program ○ IO, Free, ... 世の中には2種類の関数型プログラミングのテクニックがあります。純 粋なプログラムのためのものと、作用を扱うものです。

Slide 10

Slide 10 text

There are 2 types of FP techniques ● For a pure program <- EASY ○ val, immutable, ... ● For a effectful program <- HARD ○ IO, Free, … 前者に比べて、後者は比較的難しいと思っています。

Slide 11

Slide 11 text

Let’s start with FP for a pure program なので、純粋なプログラムのための関数型プログラミングから始めま しょう。

Slide 12

Slide 12 text

Partially applied Functional Programming for us ● Basic strategy ● What we’ve found 私達の「部分的な関数型プログラミング」について、基本的な戦略と、 やってみてわかったことについて話します。

Slide 13

Slide 13 text

Basic strategy 基本的な戦略について話します。

Slide 14

Slide 14 text

1: Recognize inevitable effects 1: 避けられない作用を認識します。

Slide 15

Slide 15 text

1: Recognize inevitable effects 1: 避けられない作用を認識します。

Slide 16

Slide 16 text

2: Find out parts that don’t depend on effects 2: 作用に依存しない部分を見つけます。

Slide 17

Slide 17 text

3: Extract them as pure functions 3: その部分を純粋関数として抜き出します。

Slide 18

Slide 18 text

4: Still has side effects? It’s OK! It’s better! 4: まだ作用が残っていますが、良しとしましょう。前よりはマシです。

Slide 19

Slide 19 text

What we’ve found 私達が発見したことについて話します。

Slide 20

Slide 20 text

2 layers appeared ● Pure layer ○ Independent pure package ■ Consists of pure components ■ FP world. Easy to test, reuse, etc. ○ FP libraries are heavily used here ■ e.g. Cats 2つの層が浮き彫りになりました。1つは純粋層です。独立した純粋な パッケージで、関数型の恩恵を受けることができます。

Slide 21

Slide 21 text

2 layers appeared ● Effectful layer ○ Treats DB access and other effects. ○ Depends on the pure layer ○ Most dependent libraries are used here ■ e.g. scalikejdbc もう1つは作用層で、DBアクセスなどの作用が実行されます。純粋層 に依存してロジックを実現します。

Slide 22

Slide 22 text

Dependency direction 依存の方向です。作用層は純粋層に依存しますが、逆はありません。 Effectful layer Pure layer The effectful layer depends on the pure layer, the opposite is not true.

Slide 23

Slide 23 text

Looks like this Effectful layer Depends on the pure layer こんな感じです。

Slide 24

Slide 24 text

The pure layer has another aspect ● Represents our core business rules ○ Removal of effects emphasizes our core business rules ○ Of course, they’re all easy to test, reuse, ... ● We’ve started to focus on “What is our core business rule” as well as “What is pure” また純粋層は、作用を取り除いた結果、核となるビジネスルールを表 現するようになりました。関数型のメリットは残したまま。

Slide 25

Slide 25 text

Looks like the core business logic, doesn’t it? この純粋関数の部分は、FizzBuzzの核となるビジネスロジックに見え ますよね。

Slide 26

Slide 26 text

The next step ● Need to treat core business effects? ○ It might be time to graduate “Partially applied”, and start the original FP. ○ Suit your project! 事業のコアとなる作用を扱いたくなったら?関数型の部分適用は卒業 して、本来の関数型を始めるときかもしれません。

Slide 27

Slide 27 text

Recap まとめです。

Slide 28

Slide 28 text

Recap ● It’s beneficial to apply FP partially. ● Extract pure methods, and you’ll find it’s your core business rules. References 関数型を部分的に適用するのも有用です。純粋関数を抽出すると、そ れが核となるビジネスロジックだと気付きます。 ● Functional Programming in Scala ● The Clean Architecture

Slide 29

Slide 29 text

Thanks for listening! ご静聴ありがとうございました!