问题描述:

In scala is there an easy way to transform an optional tuple into a tuple of options i.e

Option[(Int, Int)] => (Option[Int], Option[Int])

Thanks

p.s. now I am just using:

val myTuple: Option[(Int, Int)] = Some((1, 1))

if (myTuple.isDefined)

(Some(myTuple.get._1), Some(myTuple.get._2))

else

(None, None)

网友答案:

Something like this maybe (not that it is much "easier" than what you have, but more idiomatic):

 option.map { case (a,b) => Some(a) -> Some(b) }.getOrElse(None -> None)
网友答案:
val myTuple: Option[(A, B)] = ...
(myTuple.map(_._1), myTuple.map(_._2))
相关阅读:
Top