[英]Filter, reduce, map with lodash _.flow
As a beginner applying the lodash library in the functional programming, I found that ._flow
doesn't excute the functions put in. My question is how do I apply map, reduce, or filter inside the flow?作为在函数式编程中应用 lodash 库的初学者,我发现
._flow
不执行放入的函数。我的问题是如何在流中应用 map、reduce 或 filter? Or anything that I did wrong?或者我做错了什么? Any help is highly appreciated.
非常感谢任何帮助。
For example:例如:
const _=require('lodash');
const scores = [50, 6, 100, 0, 10, 75, 8, 60, 90, 80, 0, 30, 110];
const newArray=_.filter(scores,val=>val<=100);
const newArray2=_.filter(newArray,val=>val>0);
console.log(newArray2);
// the output is
/*[
50, 6, 100, 10, 75,
8, 60, 90, 80, 30
]*/
However, when I make it two separate functions and put into flows, it doesn't do anything.然而,当我把它变成两个独立的函数并放入流程中时,它什么也没做。
const newRmvOverScores=_.filter(val=>val<=100);
const newRmvZeroScores=_.filter(val=>val>0);
const aboveZeroLess100=_.flow(newRmvOverScores,newRmvZeroScores)(scores);
console.log(aboveZeroLess100);
// the output is:
/*[
50, 6, 100, 0, 10, 75,
8, 60, 90, 80, 0, 30,
110
]*/
Several references I've found:我发现的几个参考资料:
[1] Using lodash, why is the map method within flow not working? [1] 使用lodash,为什么flow内的map方法不起作用? [2] https://lodash.com/docs/4.17.15#flow
[2] https://lodash.com/docs/4.17.15#flow
Two issues:两个问题:
newRmvOverScores
and newRmvZeroScores
, you actually already execute _.filter
, and without a collection argument.newRmvOverScores
和newRmvZeroScores
时,您实际上已经执行_.filter
并且没有集合参数。 They should be functions_.flow
expects an array of functions, but you don't provide an array, nor are the arguments functions (because of the previous point) _.flow
需要一个函数数组,但你没有提供数组,arguments 函数也没有(因为前一点) Here is the corrected script:这是更正后的脚本:
const scores = [50, 6, 100, 0, 10, 75, 8, 60, 90, 80, 0, 30, 110]; const newRmvOverScores = scores => _.filter(scores, val=>val<=100); const newRmvZeroScores = scores => _.filter(scores, val=>val>0); const aboveZeroLess100 = _.flow([newRmvOverScores,newRmvZeroScores])(scores); console.log(aboveZeroLess100);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js" integrity="sha512-90vH1Z83AJY9DmlWa8WkjkV79yfS2n2Oxhsi2dZbIv0nC4E6m5AbH8Nh156kkM7JePmqD6tcZsfad1ueoaovww==" crossorigin="anonymous"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.