I have two Options
val opt1 = Some(List("Sal", "Salil"))
val opt2 = Some(List("Sal2", "Salil2"))
Either opt1 or opt2 could be None. If either one is None then I want an option with the List contained in the other. If both are None then None should be returned.
If both are Some then a Some with a list containing elements from both lists as shown below:
Some(List(Sal, Salil, Sal2, Salil2))
I know I can do this by hand, but is there an elegant way to do this? For-comprehensions don't work if one of the Options is a None.
Option((opt1 ++ opt2).flatten.toList).filter(_.nonEmpty)
You can do this nicely using semigroup appending with scalaz or cats:
import scalaz._, Scalaz._ // for cats use `import cats._, implicits._`
val opt1 = Option(List("Sal", "Salil"))
val opt2 = Option(List("Sal2", "Salil2"))
scala> opt1 |+| opt2
res0: Option[List[String]] = Some(List(Sal, Salil, Sal2, Salil2))
scala> opt1 |+| None
res1: Option[List[String]] = Some(List(Sal, Salil))
scala> Option.empty[List[String]] |+| None
res2: Option[List[String]] = None
Otherwise, with the standard library you may need to handle it case-by-case:
(opt1, opt2) match {
case (Some(a), Some(b)) => Option(a ++ b)
case (Some(a), None) => Option(a)
case (None, Some(b)) => Option(b)
case _ => None
}
Or use collection methods to flatten them down:
scala> List(opt1, opt2).flatten.flatten
res5: List[String] = List(Sal, Salil, Sal2, Salil2)
scala> List(opt1, None).flatten.flatten
res6: List[String] = List(Sal, Salil)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With