Search code examples
scalafunctional-programmingscala-option

Using scala.Option functionally


I have an Option, say O, which can either be None or may have some value inside. If it has some value, that value may have a flag, say f. My requirement is that if O is None, then I create an object, say of type MyEntity,but if O has a value with flag as true, I return Nil else I create instance of MyEntity with different value. Java code can be almost as:

if(O.isEmpty) {
  MyEntity("x")
} else {
  if(O.f) {
     Nil
   } else {
    MyEntity("y") // with different value
   }
}

I want to do this in functional style using HoFs provided in scala.Option. What would be best possible way of doing it? I could this so far :

if(O.isEmpty){
 MyEntity("x")
} else {
 Option.unless(O.exists(_.f))(MyEntity("y"))
}

Solution

  • I misread your question the first go round, so here is attempt #2

    This is a great case for pattern matching:

    
    val maybeMyEntity: Option[MyEntity] = option match {
        case Some(flagValue) if flagValue => None
    //  case Some(true) => None (More concise, but does not highlight a handy feature)
        case Some(_) => Some(MyEntity("Y"))
        case None => Some(MyEntity("X"))
    }
    

    Pattern matching is very powerful.

    Alternatively, mapping is another option:

    mapping of an Option will only occur if its value is not empty, and flatMapping will remove the layer of Option added, from Option[Option[MyEntity]] to Option[MyEntity]

    val result: Option[MyEntity] = if (option.isEmpty) {
        Some(Entity("X"))
    } else {
        option.flatMap { flagValue =>
            if (flagValue) {
                None
            } else {
                Some(MyEntity("Y"))
            }
        }
    }