47

What's the most idiomatic way to test whether all items of a collection match a predicate?

Any item?

ripper234
  • 222,824
  • 274
  • 634
  • 905

1 Answers1

81

There are built-in functions for this:

List(1,2,3,4).forall(x => x < 5)
res0: Boolean = true

for any:

List(1,2,3,4).exists(x => x > 3)
res1: Boolean = true
Luke Francl
  • 31,028
  • 18
  • 69
  • 91
om-nom-nom
  • 62,329
  • 13
  • 183
  • 228