JavaScript中如何实现函数组合?
以下是需要组合并为我们提供输出 30
的三个函数:
const add = (a) => a + 10;
const mul = (a) => a * 10;
const divide = (a) => a / 5;
// How to implement `compositionFunction`?
compositionFunction(add, mul, divide)(5);
//=> 30
预期输出为 30
,因为:
5 + 10 = 15
15 * 10 = 150
150 / 5 = 30
Below are three functions that need to be composed and give us the output 30
:
const add = (a) => a + 10;
const mul = (a) => a * 10;
const divide = (a) => a / 5;
// How to implement `compositionFunction`?
compositionFunction(add, mul, divide)(5);
//=> 30
Expected output is 30
because:
5 + 10 = 15
15 * 10 = 150
150 / 5 = 30
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
像这样的东西
Something like this
函数组合有两种风格:
管道
compose
这是一个仅供娱乐的递归实现:
这假设至少有两个函数要组合
让我们尝试一下:
There are two flavours of function composition:
pipe
compose
Here's a recursive implementation just for fun:
This assumes that there are at least two functions to compose
Let's try:
这就是我要做的:
Here's what I would do: