当前位置: 动力学知识库 > 问答 > 编程问答 >

scala - Optional tuple to tuple of options

问题描述:

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))
分享给朋友:
您可能感兴趣的文章:
随机阅读: