uraza
uraza

Reputation: 957

Do something if option is None or value satisfies condition

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

Answers (1)

Dima
Dima

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

Related Questions