简体   繁体   English

使用compose函数和Array.reduce进行JavaScript函数编程

[英]JavaScript functional programing with compose function and Array.reduce

I am new to JavaScript Functional programming. 我是JavaScript Functional编程的新手。 In code below, compose can't work without setInterval outside it and clear as the first argument also does't give the compose initial value. 在下面的代码中,如果没有setIntervalcompose就无法工作并clear因为第一个参数也没有给出compose初始值。

So my question is how can compose work without the setInterval ? 所以我的问题是如何在没有setInterval情况下compose工作?

 const clear = () => console.clear() const f1 = () => 2 const log = message => console.log(message) const compose = (...fns) => arg => fns.reduce( (composed, f) => f(composed), arg ) setInterval( compose(clear, f1, log), 1000 ) 

compose(...fns) returns a function. compose(...fns)返回一个函数。 When used with setInterval , it is being called implicitly by the JavaScript engine. setInterval使用时,JavaScript引擎会隐式调用它。

If you want to use it directly, you can do something like: 如果您想直接使用它,您可以执行以下操作:

 const clear = () => console.clear() const f1 = () => 2 const log = message => console.log(message) const compose = (...fns) => arg => fns.reduce( (composed, f) => f(composed), arg ) compose(clear, f1, log)(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM