11. 什么是Composition?实现pipe()
分享给更多人
容易 -通过 / -执行
什么是Composition? 其实并不难理解,看看@dan_abramov 's 的说明就知道了。
现在需要你自己写一个pipe()
方法。
假设有一些简单的四则运算方法:
const times = (y) => (x) => x * y
const plus = (y) => (x) => x + y
const subtract = (y) => (x) => x - y
const divide = (y) => (x) => x / y
pipe()
可以用来生成新的计算方式
pipe([
times(2),
times(3)
])
// x * 2 * 3
pipe([
times(2),
plus(3),
times(4)
])
// (x * 2 + 3) * 4
pipe([
times(2),
subtract(3),
divide(4)
])
// (x * 2 - 3) / 4
注意
- 为了简单,可以假设传给
pipe()
的方法都只有一个参数