Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to compare value obtained from Option[Long] scala

Tags:

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.

like image 267
Priya Avatar asked Jan 27 '26 12:01

Priya


2 Answers

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
like image 104
Ren Avatar answered Jan 30 '26 10:01

Ren


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)
like image 35
Noah Avatar answered Jan 30 '26 11:01

Noah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!