Search code examples
scalafunctional-programmingscala-option

Correct way to work with two instances of Option together


When I have one Option[T] instance it is quite easy to perform any operation on T using monadic operations such as map() and flatMap(). This way I don't have to do checks to see whether it is defined or empty, and chain operations together to ultimately get an Option[R] for the result R.

My difficulty is whether there is a similar elegant way to perform functions on two Option[T] instances.

Lets take a simple example where I have two vals, x and y of type Option[Int]. And I want to get the maximum of them if they are both defined, or the one that is defined if only one is defined, and None if none are defined.

How would one write this elegantly without involving lots of isDefined checks inside the map() of the first Option?


Solution

  • You can use something like this:

    def optMax(op1:Option[Int], op2: Option[Int]) = op1 ++ op2 match {    
      case Nil => None  
      case list => list.max
    }
    

    Or one much better:

    def f(vars: Option[Int]*) = (for( vs <- vars) yield vs).max
    

    @jwvh,thanks for a good improvement:

    def f(vars: Option[Int]*) = vars.max