gpt4 book ai didi

javascript - Ramda - R.Both 具有采用不同数量参数的函数

转载 作者:行者123 更新时间:2023-12-01 02:38:38 26 4
gpt4 key购买 nike

刚刚学习 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com