generated at
カリー化と部分適用
カリー化とは?
2つ以上の引数を持つ関数を分解、関数を戻り値とする引数が1つの関数の入れ子にすること。
JavaScript
const square = bottom => { return height => { return bottom * height; } } square(4)(5); // 結果 20

部分適用とは?
引数の一部を定数化して新しい関数を作り出すこと。
JavaScript
const square = bottom => { return height => { return bottom * height; } } // ここで底辺が5で固定の関数を生成 const squareBottom5 = square(5); squareBottom5(10); // 結果 50

カリー化すると何が嬉しいのか?
部分適用した関数が作りやすくなる。
処理が似たような関数を組み替えやすく、共通化に繋がる。

部分適用すると何が嬉しいのか?
引数が少なく単純で分かりやすい関数になる。

具体的な使われ方
fetch APIが非常に分かりやすい
JavaScript
const ajax = method => url => body => fetch(url, { method, body }) const post = ajax('POST') const get = ajax('GET') const getTags = get('https://qiita.com/api/v2/tags?page=1') getTags() .then(res => res.json()) .then(console.log) const createUser = post('https://example.com/create/user') createUser({ name: 'yametarou' }) .then(res => res.json()) .then(console.log)