有没有办法使用 ramda 将参数传递给 JavaScript 中的谓词?
Is there a way to pass a parameter to a predicate in JavaScript using ramda?
我在 JavaScript 中有一个带有 ramda 库的功能代码。我想要一个通用函数 hasNChars
并动态传递参数 n
。我不能做 R.any(hasNChars(10), words)
因为函数被评估了。
那么有没有办法以某种方式传递 n 参数的值?
var R = require('ramda');
let words = ['forest', 'gum', 'pencil', 'wonderful', 'grace',
'table', 'lamp', 'biblical', 'midnight', 'perseverance',
'adminition', 'redemption'];
let hasNChars = (word, n=3) => word.length === n;
let res = R.any(hasNChars, words);
console.log(res);
你很接近,你只需要创建另一个接受 N 的函数,你可以立即评估而无需输入 word
,这样 N 值就在最终评估的范围内。
let hasNChars = (n=3) => (word) => word.length === n;
用法:
let res = R.any(hasNChars(10), words);
默认 n=3 的用法:
let res = R.any(hasNChars(), words);
我在 JavaScript 中有一个带有 ramda 库的功能代码。我想要一个通用函数 hasNChars
并动态传递参数 n
。我不能做 R.any(hasNChars(10), words)
因为函数被评估了。
那么有没有办法以某种方式传递 n 参数的值?
var R = require('ramda');
let words = ['forest', 'gum', 'pencil', 'wonderful', 'grace',
'table', 'lamp', 'biblical', 'midnight', 'perseverance',
'adminition', 'redemption'];
let hasNChars = (word, n=3) => word.length === n;
let res = R.any(hasNChars, words);
console.log(res);
你很接近,你只需要创建另一个接受 N 的函数,你可以立即评估而无需输入 word
,这样 N 值就在最终评估的范围内。
let hasNChars = (n=3) => (word) => word.length === n;
用法:
let res = R.any(hasNChars(10), words);
默认 n=3 的用法:
let res = R.any(hasNChars(), words);