73

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.

5
  • 5
    If you think Haskell is 99% SML, you either haven't gotten to monads yet or rate underlying principles way higher than the way actual code looks and works like (e.g. you'd also consider Java 99% C++).
    – user395760
    Jun 17, 2011 at 19:29
  • how does haskell or SML are related to this question ( perhaps I'm missing the obvious )
    – OscarRyz
    Jun 17, 2011 at 19:33
  • 1
    well, let’s say that 99% of SML is in Haskell ;) Jun 17, 2011 at 19:33
  • i remember writing similar (=functional) code in SML. That’s why i’m not completely alien to functional programming and not disturbed when having to pass a function as parameter to another) Jun 17, 2011 at 19:35
  • 1
    Haskell and SML are quite different since Haskell is lazy by default.
    – Kim Stebel
    Jun 17, 2011 at 20:29

4 Answers 4

133
  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.

5
  • a for loop, how conventional :). I don’t know if you brought up exists and forall first (@rafalotufo’s answer was edited to include it). how should i behave in this case? can’t accept both. Jun 17, 2011 at 19:51
  • 6
    In TraversableLike, exists is short-circuited.
    – ziggystar
    Jun 17, 2011 at 21:28
  • I wonder if there's also a built in .all with no arguments. Sep 18, 2013 at 14:52
  • @ErikAllik, there isn't. Sep 19, 2013 at 8:04
  • 1
    @ErikAllik it would be nice to have an .all with no arguments for Traversable[Boolean], but I was not able to find it.
    – Trylks
    Aug 11, 2014 at 19:38
6

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
2
  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.

4
  • Thanks, I’ll accept this one as soon as Stackoverflow lets me do so. Double thanks for the hint on early exiting. An idea how to do it elegantly? Jun 17, 2011 at 19:38
  • And someTraversable.reduceLeft(_+_) is a bit clumsier than someTraversable.sum, but I don’t think it’s worth to define a function (unlike for any and all, which seem to be quite a bit lengthier). Note that I would like to see sum, product and stuff in scala.Predef, too, only I’d not define it myself if it isnNt there. Jun 17, 2011 at 19:43
  • 2
    The standard library does have the sum function. (Resides in GenTraversableOnce.) Jun 17, 2011 at 19:45
  • @flying sheep: the most elegant way would be to just use the exists function, which probably checks for that. If you want to implement it your self, you will need to use standard looping mechanism and break the loop when you found true.
    – rafalotufo
    Jun 17, 2011 at 21:27
1

How about exists:

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

It's on Traversable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.