Priya
Priya

Reputation: 141

how to compare value obtained from Option[Long] scala

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

Answers (2)

Ren
Ren

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

Noah
Noah

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

Related Questions