【发布时间】:2012-08-05 22:39:02
【问题描述】:
我正在尝试将两个 Option[Iterable[_]] 组合成一个新的 Option[Iterable[_]]。如果其中一个(或两个)元素是 Some ,我想返回 Some ,否则返回 None 。似乎应该有一种惯用的方式来做到这一点,但我似乎找不到。以下似乎可以满足我的要求,但并不是我希望的完美解决方案。
def merge(
i1: Option[Iterable[_]], i2: Option[Iterable[_]]
): Option[Iterable[_]] = (i1, i2) match {
case (Some(as), Some(bs)) => Some(as ++ bs)
case (a @ Some(as), None) => a
case (None, b @ Some(bs)) => b
case _ => None
}
感谢任何提示。谢谢!
【问题讨论】:
-
类似的问题:stackoverflow.com/questions/10617979/…,可能会有帮助
标签: scala collections monads