Reputation: 141
This is the code snippet:
OS.config flatMap {_.Allocation } flatMap {_.memory}
The value obtained from the memory
parameter is Option[Long]
.
How can I compare and check if it is greater than zero?
I tried using filter, but the answer obtained is Option[Boolean]
. I need to check for many objects and if the value is greater than zero increment the counter.
Upvotes: 3
Views: 2299
Reputation: 3455
You could use the exists
method. This method, when run on an option, takes a function that returns a boolean. If your Option is a None, it will return false.
eg.
scala> var x = Some(123)
x: Some[Int] = Some(123)
scala> x.exists(_ > 0)
res0: Boolean = true
scala> x.exists(_ < 0)
res1: Boolean = false
Upvotes: 7
Reputation: 13959
You can fold
over it to either get false
if it hasn't been specified or check if it's greater than 0
:
(OS.config flatMap {_.Allocation } flatMap {_.memory}).fold(false)(_ > 0)
Upvotes: 2