Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining two Option[List[String]] in Scala

Tags:

scala

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.

like image 801
Salil Surendran Avatar asked Mar 01 '17 20:03

Salil Surendran


2 Answers

Option((opt1 ++ opt2).flatten.toList).filter(_.nonEmpty)
like image 173
Alvaro Carrasco Avatar answered Sep 28 '22 06:09

Alvaro Carrasco


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)
like image 38
Michael Zajac Avatar answered Sep 28 '22 04:09

Michael Zajac