Scala:“任何”和“所有”函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6390797/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 03:06:38  来源:igfitidea点击:

Scala: “any” and “all” functions

scalafunctional-programming

提问by flying sheep

my Haskell* is a bit rusty, so i can imagine that I'm missing the obvious:

我的 Haskell* 有点生疏,所以我可以想象我错过了明显的:

def any[A](s: Traversable[A], f: A => Boolean): Boolean = {
    s.foldLeft(false)((bool, elem) => bool || f(elem))
}

Does one of these properties apply to the it?

这些属性之一是否适用于它?

  1. predefined somewhere in the Scala libs
  2. circumstantial, and faster written as some one-liner
  3. wrong (I didn't test it, sorry ;))
  1. 在 Scala 库中的某处预定义
  2. 间接的,更快地写成一些单行
  3. 错误(我没有测试过,抱歉;))


*actually SML, but that's 99% the same, but known by nobody under the sun.

*实际上是 SML,但 99% 相同,但在阳光下无人知晓。

回答by missingfaktor

  1. It's predefined and is called exists. And forallwould be the "all" function you are looking for.

    scala> Vector(3, 4, 5).exists(_ % 2 == 0)
    res1: Boolean = true
    
    scala> Vector(3, 4, 5).forall(_ % 2 == 0)
    res2: Boolean = false
    
  2. You can make it more performant using a forloop with a break(from scala.util.control.Breaks). (See the standard library implementation of existsand forall.)

  3. It's correct.

  1. 它是预定义的,称为exists. 并且forall将是您正在寻找的“全部”功能。

    scala> Vector(3, 4, 5).exists(_ % 2 == 0)
    res1: Boolean = true
    
    scala> Vector(3, 4, 5).forall(_ % 2 == 0)
    res2: Boolean = false
    
  2. 您可以使用for带有break(from scala.util.control.Breaks)的循环提高性能。(参见标准库实现的existsforall。)

  3. 这是正确的。

回答by David Winslow

Methods exist on the Traversable trait which are equivalent to anyand all:

Traversable trait 中存在的方法等价于anyall

def all[A](xs: Traversable[A], p: A => Boolean): Boolean = xs forall p

def any[A](xs: Traversable[A], p: A => Boolean): Boolean = xs exists p

回答by rafalotufo

  1. No it isn't predifined with those names. You can use existsfrom Traversable package.
  2. The biggest disadvantage of your implementation is that will necessary consume all of your traversible, when, for any, if any is true, if could already give you your answer. The same goes for all. But one could easily implement this so that it doesn't evaluate the whole sequence. Another solution would be to implement a monad for this type of operation. Then you would call:

    a and b and cwhich is equivalent to a.and(b).and(c)

  3. It is correct.

  1. 不,它没有预先定义这些名称。您可以exists从 Traversable 包中使用。
  2. 您的实现的最大缺点是将有必要消耗您所有的可遍历对象,当,for any,如果为真,如果已经可以给您答案。也是如此all。但是人们可以很容易地实现这一点,这样它就不会评估整个序列。另一种解决方案是为这种类型的操作实现一个 monad。然后你会打电话:

    a and b and c这相当于 a.and(b).and(c)

  3. 它是正确的。

BTW, another function that I find missing is a sumfunction.

顺便说一句,我发现缺少的另一个功能是一个sum功能。

回答by Adam Rabung

How about exists:

怎么样exists

scala> List(1,2,3).exists(_ > 2)
res12: Boolean = true

It's on Traversable.

它在Traversable 上