# Composition

會拆分成許多小 function ,每一個 function 就像一根根小水管,小水管之間互相獨立,也遵守 one input, one output 概念 (Pure),最後再接再一起。

範例部分可以瀏覽 參考連結 (opens new window)

# Pipe + Curry

const toUpper = (str) => str.toUpperCase();
const toLower = (str) => str.toLowerCase();
const isString = (str) => (typeof str === 'string' ? str : 'Not a string');
const add = R.curry((symbol, str) => str + symbol);

R.pipe(isString, add('!'), toUpper)('hello world'); // HELLO WORLD!
1
2
3
4
5
6

# 解析 add() 的運作

因為 pipe 是以下方實做:

const pipe = (...fns) => (x) => fns.reduce((v, f) => f(v), x);
1

add(x) 傳入 pipe 時,會是:

add(x)(lastValue);
1

# 參考

Composition (opens new window)

Last Updated: 2021/2/25 上午8:00:30