Scala:“任何”和“全部”;功能

发布于 2024-11-16 11:43:29 字数 349 浏览 1 评论 0原文

我的 Haskell* 有点生锈了,所以我可以想象我错过了显而易见的事情:

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

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

  1. 在Scala libs
  2. 环境中的某个地方预定义的,并且更快地写成一些单行
  3. 错误(我没有测试它,抱歉;))

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

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

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 ;))

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

听,心雨的声音 2024-11-23 11:43:29
  1. 它是预定义的,称为存在forall 将是您正在寻找的“all”函数。

    scala> Vector(3, 4, 5).exists(_ % 2 == 0)
    res1: 布尔值 = true
    
    标量> Vector(3, 4, 5).forall(_ % 2 == 0)
    res2: 布尔值 = false
    
  2. 您可以使用带有 breakfor 循环(来自 scala.util.control.Breaks)来提高其性能。 (请参阅 existsforall 的标准库实现。)

  3. 这是正确的。

  1. It's predefined and is called exists. And forall would 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 for loop with a break (from scala.util.control.Breaks). (See the standard library implementation of exists and forall.)

  3. It's correct.

半葬歌 2024-11-23 11:43:29

Traversable 特征上存在的方法相当于 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

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

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
那片花海 2024-11-23 11:43:29
  1. 不,它不是用这些名称预先定义的。您可以使用 Traversable 包中的 exists
  2. 您的实现的最大缺点是,当对于任何any,如果有任何为真,如果已经可以给您答案时,将需要消耗您的所有可遍历性。对于全部也是如此。但我们可以很容易地实现这一点,这样它就不会评估整个序列。另一种解决方案是为此类操作实现一个 monad。然后你会调用:

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

  3. 这是正确的。

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

  1. No it isn't predifined with those names. You can use exists from 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 c which is equivalent to a.and(b).and(c)

  3. It is correct.

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

没有心的人 2024-11-23 11:43:29

exists 怎么样:

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

它位于 Traversable< /a>.

How about exists:

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

It's on Traversable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文