Working with Options in Scala and Play Framework, is there a more concise way of checking x amount of variables like so?
if (a.isDefined || b.isDefined || c.isDefined || d.isDefined ...) {
}
Is a one liner something like (a,b,c,d).isDefined
possible?
Thanks
On top of my head, probably there's a nicer way:
List(a, b, c, d).exists(_.isDefined)
For and
s (from Rob Starling comment):
List(a, b, c, d).forall(_.isDefined)
You could also have more complex condition compositions:
// (a || b) && (c || d)
List(
List(a, b).exists(_.isDefined),
List(c, d).exists(_.isDefined)
).forall(identity)
// (a && b) || (c && d)
List(
List(a, b).forall(_.isDefined),
List(c, d).forall(_.isDefined)
).exists(identity)
And so on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With