2 ms·
Indeed. However in Scala at least zip ends up giving you a List instead of an Option. for {} yield seems to do the trick scala> val a = Some("A") a:
by almostzip 7y ago
Indeed. However in Scala at least zip ends up giving you a List instead of an Option.
for {} yield seems to do the trick
scala> val a = Some("A")
a: Some[String] = Some(A)
scala> val b = Some(1)
b: Some[Int] = Some(1)
scala> val b2 = None
b2: None.type = None
scala> for { x <- a; y <- b } yield (x,y)
res1: Option[(String, Int)] = Some((A,1))
scala> for { x <- a; y <- b2 } yield (x,y)
res2: Option[(String, Nothing)] = None
scala> a zip b
res3: Iterable[(String, Int)] = List((A,1))
scala> a zip b2
res4: Iterable[(String, Nothing)] = List()