gpt4 book ai didi

Scala 使多选项检查更加简洁

转载 作者:行者123 更新时间:2023-12-04 22:04:41 25 4
gpt4 key购买 nike

使用 Scala 和 Play 框架中的选项,是否有更简洁的方法来检查 x 数量的变量?

if (a.isDefined || b.isDefined || c.isDefined || d.isDefined ...) {

}

(a,b,c,d).isDefined 这样的单衬是可能的吗?

谢谢

最佳答案

在我的头顶上,可能有更好的方法:

List(a, b, c, d).exists(_.isDefined)

对于 and s(来自 Rob Starling 评论):
List(a, b, c, d).forall(_.isDefined)

您还可以有更复杂的条件组合:
// (a || b) && (c || d)
List(
List(a, b).exists(_.isDefined),
List(c, d).exists(_.isDefined)
).forall(identity)

// (a && b) || (c && d)
List(
List(a, b).forall(_.isDefined),
List(c, d).forall(_.isDefined)
).exists(identity)

等等。

关于Scala 使多选项检查更加简洁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25084466/

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