scala Scala用if else返回布尔值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34241404/
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
Scala return boolean with if else
提问by CodeZer0
So I have to following scenario:
所以我必须遵循以下场景:
def check(): Boolean = {
for ((digit1,digit2,digit3) <- SetOfDigits){
if ((1,2,5) == (digit1,digit2,digit3))
true
else
false
}
}
val SetOfDigits = Set((0,2,3),(1,5,6),(7,10,2),(1,2,5))
Now the problem is that the function has to return a Boolean but it always tells me that the return type here is Unit?
The function should iterate over the SetOfDigitsand if it finds something equal like (1,2,5)it should return true else false?
Has anybody an answer to this problem and what I have to do in order to get it working?
现在的问题是该函数必须返回一个布尔值,但它总是告诉我这里的返回类型是Unit? 函数应该迭代SetOfDigits,如果它找到相等的东西,(1,2,5)它应该返回真否则假?有没有人回答这个问题,我必须做什么才能让它工作?
采纳答案by Ende Neu
I don't agree with the solution from Mr. V., I'd rather suggest you change your implementation, that seems a very Javish way of handling things:
我不同意 V 先生的解决方案,我宁愿建议您更改您的实现,这似乎是一种非常 Javish 的处理方式:
scala> val SetOfDigits = Set((0,2,3),(1,5,6),(7,10,2),(1,2,5))
SetOfDigits: scala.collection.immutable.Set[(Int, Int, Int)] = Set((0,2,3), (1,5,6), (7,10,2), (1,2,5))
scala> SetOfDigits.contains((1, 2, 5))
res0: Boolean = true
scala> SetOfDigits.contains((1, 2, 4))
res1: Boolean = false
containsfind an element in your set, if the element is not there, it returns false, looks much better in my opinion.
contains在您的集合中找到一个元素,如果该元素不存在,则返回 false,在我看来看起来好多了。
In response to your comment, I'd flatten the first list and then use forAlland contains:
为了回应您的评论,我会将第一个列表展平,然后使用forAlland contains:
scala> val setOfDigits1 = Set((0,2,3),(1,5,6),(7,10,2),(1,2,5)).flatMap { case(a,b,c) => Set(a,b,c)}
setOfDigits1: scala.collection.immutable.Set[Int] = Set(0, 5, 10, 1, 6, 2, 7, 3)
scala> val setOfDigits2 = Set(1,2,3,16,20,7)
setOfDigits2: scala.collection.immutable.Set[Int] = Set(20, 1, 2, 7, 3, 16)
scala> val setOfDigits3 = Set(1,2,3,10)
setOfDigits3: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 10)
scala> setOfDigits2.forall(i => setOfDigits1.contains(i))
res8: Boolean = false
scala> setOfDigits3.forall(i => setOfDigits1.contains(i))
res9: Boolean = true
Note that I have flattend the first list from a List[(Int, Int, Int)]to a List[Int], then forAllevaluates a predicate which must be true for all elements and containsdoes the rest.
请注意,我已将第一个列表从 a 展平List[(Int, Int, Int)]到 a List[Int],然后forAll评估一个谓词,该谓词必须对所有元素都为真,然后contains执行其余的操作。
回答by vvg
You should add returnstatement before true or falseto stop on first execution.
您应该return在 true 之前添加语句或false在第一次执行时停止。
Otherwise you can use yieldto collect results from each of sub-set (you'll get list of Booleans)
否则,您可以使用yield从每个子集收集结果(您将获得布尔值列表)
Depends what is your expectation. If you want to make sure at leaston of sub-sets satisfying results, your function will looks like following:
取决于你的期望是什么。如果您想确保至少有一个子集满足结果,您的函数将如下所示:
def check(): Boolean = {
val s = for ((digit1,digit2,digit3) <- SetOfDigits) {
if ((1,2,5) == (digit1,digit2,digit3))
return true
}
false
}
回答by elm
As mentioned, yield the result from the if-else, otherwise the for comprehension returns Unit. In a similar way to @EndeNeu API based method, consider also
如前所述,yield 结果来自if-else,否则 for comprehension 返回Unit。与基于@EndeNeu API 的方法类似,还要考虑
xs.exists( _ == (1,2,5) )
Boolean = true
which halts the iteration over the collection as the first match is found.
在找到第一个匹配项时停止对集合的迭代。
Note also by convention the tag of a collection starts with a lower case letter, namely setOfDigitsinstead of SetOfDigits, while types and classes are capitalised.
另请注意,按照惯例,集合的标签以小写字母开头,即setOfDigits代替SetOfDigits,而类型和类则大写。
回答by Kigyo
I agree with Ende Neu's answer.
我同意 Ende Neu 的回答。
In the general case you can introduce a boolean variable that you check inside the for-comprehension to end it prematurely. I prefer it over using the returnkeyword.
在一般情况下,您可以引入一个布尔变量,您可以在 for-comprehension 中检查该变量以提前结束它。我更喜欢它而不是使用return关键字。
def check(): Boolean = {
var found = false
for ((digit1,digit2,digit3) <- SetOfDigits if !found) {
if ((1,2,5) == (digit1,digit2,digit3)) found = true
}
found
}

