(任一个)是否有可变参数版本? [英] Is there a Variadic Version of either (R.either)?
本文介绍了(任一个)是否有可变参数版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要变体版本的 R.two
.在网上搜索后,我没有找到解决方案. R.anyPass
可以使用,但是它返回一个布尔值而不是原始值.是否已经有我忽略的解决方案?如果不是,编写可变参数的两个效用函数的最佳方法是什么?
I have a need for a variadic version of R.either
. After doing some searching around the web, I have not found a solution. R.anyPass
would work but it returns a Boolean instead of the original value. Is there already a solution that I have overlooked? If not, what would be the most optimal way to write a variadic either utility function?
一个例子:
const test = variadicEither(R.multiply(0), R.add(-1), R.add(1), R.add(2))
test(1) // => 2
推荐答案
您可以结合使用 reduce
+ reduced
:
You could use a combination of reduce
+ reduced
:
const z = (...fns) => x => reduce((res, fn) => res ? reduced(res) : fn(x), false, fns);
console.log(
z(always(0), always(10), always(2))(11),
z(always(0), always(''), always(15), always(2))(11),
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js"></script>
<script>const {reduce, reduced, always} = R;</script>
(先前尝试)
我会做这样的事情:
const z = unapply(curry((fns, x) => find(applyTo(x), fns)(x)));
console.log(
z(always(0), always(15), always(2))(10),
z(always(0), always(''), always(NaN), always(30), always(2))(10),
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js"></script>
<script>const {unapply, curry, find, applyTo, always} = R;</script>
这有三个主要警告!
- 您必须通过两次通过"调用
z
,即z(... functions)(x)
- 尽管添加起来应该很容易,但是我并不关心没有函数匹配"的情况.
- 也许没什么大不了,但是值得注意:一个匹配的谓词将被执行两次
这篇关于(任一个)是否有可变参数版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文