作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
刚刚学习 Ramda,发现我不断遇到同样的情况。我有 2 个条件函数,我想用 R.both 来评估它们。不同的是,1 个函数需要一个额外的参数。像这样的东西:
const condition1 = (y) => y===1;
const condition2 = (x, y) => x===y;
const 验证器 = R.both(条件1, 条件2)
请注意,条件 2 的参数已切换。如果我知道 x
将首先应用,那么效果很好。但如果不是呢?
我会吗:
1) 改变条件 1,如 const conditionAltered = () => 条件 1
2)使用一些 Ramda 函数来执行相同的操作(不确定那会是什么)
3) ????
寻找“最佳实践”类型的答案。
<小时/>好吧,事实证明我完全误解了 R.both
的操作方式。首先,我认为它会自动柯里化(Currying)传入的条件,其次我没有意识到它短路了。
鉴于我的(1)应该读取const conditionAltered = (x, y) => condition1(y)
最佳答案
不确定问题中的条件是否正确,但我会尽力给出解决方案。
令上述条件如下。
const condition1 = (x) => x === 2;
const condition2 = (x, y) => x > y;
要检查这两个条件,您可以执行以下操作
const validator = R.both(condition1, condition2);
validator(2, 1) // return `true`
现在如果你想反转condition2
的参数,那么你可以这样做
const condition1 = (x) => x === 2;
const condition2 = (y, x) => x > y;
const validator = R.both(condition1, R.flip(condition2));
validator(2, 1) // return `true`
注意,我添加了R.flip()
翻转参数。
关于javascript - Ramda - R.Both 具有采用不同数量参数的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47718423/
我是一名优秀的程序员,十分优秀!