Reputation: 957
If I have an Option[Int]
and a function validateInt(i: Int): Boolean
. I want to do something if the option is empty or if it passes the validation.
I know it can be done with
if (opt.forall(validateInt)) {
// do something
}
Is there a more functionally idiomatic way to do this in Scala?
Upvotes: 0
Views: 1204
Reputation: 40510
opt.filterNot(validateInt).getOrElse(doStuff): Unit
But really, this does not look any better to my eye than your if
statement.
Upvotes: 4