【问题标题】:Reducing options in scala?减少scala中的选项?
【发布时间】:2014-05-19 02:42:52
【问题描述】:

最好的方法是什么:

def reduce[A](x: Option[A], y: Option[A])(f: (A, A) => A): Option[A] = 
  (x, y) match {
    case (Some(a), Some(b)) => Some(f(a, b))
    case (None, None) => None
    case (_,  None) => x
    case (None, _) => y
  }

我看了this的问题,但他的情况与我的略有不同......

【问题讨论】:

    标签: scala functional-programming reduce scalaz optional


    【解决方案1】:

    最短的大概是

    (x ++ y).reduceLeftOption(f)
    

    这是因为从 OptionIterable 的隐式转换,恰好有一个方法可以完全满足您的需求。

    【讨论】:

      猜你喜欢
      • 2016-12-05
      • 1970-01-01
      • 2015-12-07
      • 2014-08-04
      • 1970-01-01
      • 2021-12-02
      • 2018-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多