在 Scala 中,是否有一种简洁的方法来比较一个值与多个值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5927608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Scala, is there a neat and simple way to compare one value with multiple values
提问by Gigatron
Say I have a variable x, and I want to check if it's equal to any one of multiple values a, b, c, d, e (I mean the == equality, not identity).
假设我有一个变量 x,我想检查它是否等于多个值 a、b、c、d、e 中的任何一个(我的意思是 == 相等,而不是身份)。
In an SQL query the same concept is handled with
在 SQL 查询中,处理相同的概念
WHERE x IN (a, b, c, d, e).
Is there something equivalent in Scala that's as straightforward as that? I know it's otherwise possible to do it in one line with a complex expression such as building a HashSet and checking for existence in the set, but I'd prefer to use a simple construct if it's available.
在 Scala 中有没有像那样简单的等价物?我知道可以在一行中使用复杂的表达式来完成它,例如构建一个 HashSet 并检查集合中的存在,但如果它可用,我更愿意使用简单的构造。
回答by missingfaktor
You could implement an inoperator as follows:
您可以in按如下方式实现运算符:
scala> implicit def anyWithIn[A](a: A) = new {
| def in(as: A*) = as.exists(_ == a)
| }
anyWithIn: [A](a: A)java.lang.Object{def in(as: A*): Boolean}
scala> 5 in (3, 4, 9, 11)
res0: Boolean = false
scala> 5 in (3, 4, 9, 11, 5)
res1: Boolean = true
回答by Frank S. Thomas
I would prefer contains(a)over exists(_ == a):
我宁愿contains(a)超过exists(_ == a):
scala> List(3, 4, 5) contains 4
res0: Boolean = true
scala> List(3, 4, 5) contains 6
res1: Boolean = false
Update: containsis defined in SeqLike, so the above works with any sequence.
更新:contains在 中定义SeqLike,因此上述适用于任何序列。
Update 2: Here is the definition of containsin SeqLike:
更新 2:这是containsin的定义SeqLike:
def contains(elem: Any): Boolean = exists (_ == elem)
回答by oxbow_lakes
Given that a Set[A]is also a A => Boolean, you can just say:
鉴于 aSet[A]也是 a A => Boolean,你可以说:
Set(a, b, c, d, e) apply x
It's actually quite nice to define some pimpin' sugar for this:
为此定义一些 pimpin 糖实际上非常好:
class PredicateW[A](self : A => Boolean) {
def ∈:(a : A) = self apply a
}
implicit def pred2wrapper[A](p : A => Boolean) = new PredicateW(p)
Then you can write the code like so:
然后你可以像这样编写代码:
x ∈: Set(a, b, c, d, e)
回答by Malvolio
By synthesizing all the other answers, I have come up with the correctanswer:
通过综合所有其他答案,我得出了正确答案:
implicit def anyWithIn[A](a: A) = new {
def ∈(as: A*) = as.contains(a)
}
anyWithIn: [A](a: A)java.lang.Object{def ?(as: A*): Boolean}
5 ∈ (1,3,5)
res1: Boolean = true
Ta-da.
达达。
回答by user unknown
exists:
存在:
List (3, 4, 5).exists (_ == 4)
// res20: Boolean = true
find and filter come close:
find 和 filter 接近:
List (3, 4, 5).find (_ == 4)
// res16: Option[Int] = Some(4)
List (3, 4, 5).filter (_ == 4)
// res17: List[Int] = List(4)
My first answer was, as other answers, to use contain:
与其他答案一样,我的第一个答案是使用包含:
List (3, 4, 5).contains (4)
but then I thought, it would only work for boxed values like 4, not for classes, which distinguish identity and equality. To prove it, I wrote a small class, which proved mewrong: :)
但后来我想,它只适用于像 4 这样的盒装值,而不适用于区分身份和平等的类。为了证明这一点,我写了一个小班,证明我错了:)
class Ue (val i: Int) {
override def equals (other: Any) = other match {
case o: Ue => i == o.i
case _ => false }
}
val a = new Ue (4)
// a: Ue = Ue@1e040e5
val b = new Ue (4)
// b: Ue = Ue@1a4548b (no identity)
a == b
// res110: Boolean = true (surprise?)
a.equals (b)
// res112: Boolean = true (expected)
a.eq (b)
// res113: Boolean = false (expected)
List (a).contains (b)
// res119: Boolean = true (surprise)
List (a).exists (_ == b)
// res120: Boolean = true (expected)
List (a).exists (_ .eq (b))
// res121: Boolean = false (expected)
I see, I have to use equals/eq/== more often, to get the distinctions into my brain.
我明白了,我必须更频繁地使用 equals/eq/== 来区分我的大脑。
List (3, 4, 5).contains (4)
is imho the answer which is most easy.
恕我直言,这是最简单的答案。
回答by Daniel C. Sobral
Set(a, b, c, d, e)(x)works as well. I'll leave the reasons for it as an exercise to the reader. :-)
Set(a, b, c, d, e)(x)也有效。我将其原因留给读者作为练习。:-)
回答by king of codex
class Ue (val i: Int) {
override def equals (other: Any) = other match {
case o: Ue => i == o.i
case _ => false }
}
val a = new Ue (4)
// a: Ue = Ue@1e040e5
val b = new Ue (4)
// b: Ue = Ue@1a4548b (no identity)
a == b
// res110: Boolean = true (surprise?)
a.equals (b)
// res112: Boolean = true (expected)
a.eq (b)
// res113: Boolean = false (expected)
List (a).contains (b)
// res119: Boolean = true (surprise)
List (a).exists (_ == b)
// res120: Boolean = true (expected)
List (a).exists (_ .eq (b))
// res121: Boolean = false (expected)

