# 関数型言語

## 関数型言語とは

オブジェクト指向と関数型プログラミングの違い

* データと振る舞いをどう扱うか
* 副作用があるか

関数型にするには

* データと振る舞いを分離させる
* 外部に依存しないよう関数の入出力を定める
* 再帰やイテレーションメソッドを使い、副作用を取り除く

<http://qiita.com/stkdev/items/5c021d4e5d54d56b927c>

### 参照透過性

同じ関数であれば、いつ実行しても結果が同じであること。

引数が同じなら戻り値は、必ず同じであるということです

### 副作用がない

global変数の使用などによる状態の変化がないこと

ファイルのI/Oやデータベースも状態の変化に含まれてしまいますが、そこは局所化してなるべく切り分けましょう

### 関数型に書き換えるためのステップ

1. 状態(変数)と振る舞いを切り離す
2. ループではなくmapやreduceを使う
3. 振る舞いを副作用のない関数にバラす

### わかりやすい解説サイト

<http://pg-kura.hatenablog.com/entry/20111114/1321236695>

### いつ関数型プログラミングか

以下の様な兆候を感じたら、あなたはそのプログラムを関数型プログラミングで書くべきです。

"人工物"クラスが増えてきた。

Builderパターン、Strategyパターン、Commandパターン、Interpreterパターンが増えてきた。

DI注入のとき、コンストラクタ注入にするか、セッター注入にするか、いつも迷う。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yutakatay.gitbook.io/katapedia/doc/guan-shu-xing-yan-yu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
