javascript - 使用 lodash 函数映射柯里化(Currying)过滤器

标签 javascript dictionary filter lodash currying

我想做一个函数,给定一个谓词列表,生成一个数组过滤器。

使用 lodash,我定义:

const { curryRight, filter, flowRight } = require('lodash');

const curriedFilter = curryRight(filter);
const filterFromPredicates = (predicateList) => flowRight(...predicateList.map(curriedFilter));

但这给出了错误的答案:
const filter = filterFromPredicates([(x) => x > 2, (x) => x > 4])
filter([1,2,3,4,5,6,7,8,9]) // -> [1,2,3,4,5,6,7,8,9]

我期望的地方[5,6,7,8,9]
然而,定义柯里化(Currying)过滤器如下工作:
const curriedFilter = (predicate) => (array) => array.filter(predicate);

我是否误解了 curryRight 的用法? ?

最佳答案

您的代码不起作用,因为映射将 3 个参数传递给 curriedFilter - 谓词、索引和原始数组。您只需将谓词传递给 curriedFilter .

const { curryRight, filter, flow, ary } = _;

const curriedFilter = ary(curryRight(filter), 1);

const filterFromPredicates = (predicateList) => flow(...predicateList.map(curriedFilter)); // limit the number of params passed to curriedFilter

const fn = filterFromPredicates([x => x > 2, x => x < 6]); // don't override the filter you've imported

const result = fn([1,2,3,4,5,6,7,8,9]); // -> [3, 4, 5]

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>


另一种选择是限制参数的数量curriedFilter接受 _.ary() :

const { curryRight, filter, flow, ary } = _;

const curriedFilter = ary(curryRight(filter), 1);

const filterFromPredicates = (predicateList) => flow(...predicateList.map(curriedFilter)); // limit the number of params passed to curriedFilter

const fn = filterFromPredicates([x => x > 2, x => x < 6]); // don't override the filter you've imported

const result = fn([1,2,3,4,5,6,7,8,9]); // -> [3, 4, 5]

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>


您可以使用_.overEvery()生成您将传递给柯里化(Currying)过滤器的谓词:

const { flow, overEvery, curryRight, filter } = _;

const filterFromPredicates = flow(overEvery, curryRight(filter));

const fn = filterFromPredicates(x => x > 2, x => x < 6); // don't override the filter you've imported

const result = fn([1,2,3,4,5,6,7,8,9]); // -> [3, 4, 5]

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>


如果你使用 lodash/fp,你需要 _.curryRight()因为函数是自动柯里化(Currying)的,并且参数是迭代优先数据最后:

const { flow, overEvery, filter } = _;

const filterFromPredicates = flow(overEvery, filter);

const fn = filterFromPredicates([x => x > 2, x => x < 6]); // don't override the filter you've imported

const result = fn([1,2,3,4,5,6,7,8,9]); // -> [3, 4, 5]

console.log(result);
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>

关于javascript - 使用 lodash 函数映射柯里化(Currying)过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58627574/

相关文章:

javascript - 计算输入的元音?

python - 将两个列表转换为 Python 中的字典列表

javascript - 网页包错误 : document is not defined

javascript - 如何防止 ngBlur 首先触发

c# - C# 中动态字典的使用

python - 在 Python 中将文件拆分为字典

swift - 将对象(自身)传递给扩展

matlab - Matlab 中(高斯、拉普拉斯等)滤波器的可视化

javascript - 递归过滤具有不同属性的对象数组

javascript - Asp.net mvc angularjs 奇怪的延迟