In my Scala program, I am receiving some JSON.
One of the fields is an optional Boolean. If the field is missing, or if its value is false, I would like to return None
. If its value is true, I would like to return Some(true)
.
SInce this is equivalent to converting Some(false)
into None
, I have defined the following function:
def boolmap(ob: Option[Boolean]) = if(ob == Some(false)) None else ob
It works, but it doesn't seem to be very idiomatic. Is there anything more elegant?
This is ob.filter(identity)
. I'm not sure whether that's clearer, but it's shorter.
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