有没有这种解除功能的名字? [英] Is there a name for this kind of lifting a function?

查看:86
本文介绍了有没有这种解除功能的名字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个Scala函数:

pre $ def liftOrIdentity [T](f:(T,T)=> T )=(a:选项[T],b:选项[T])=>
(a,b)match {
case(Some(a),None)=>一些(a)
的情况(无,一些(b))=>一些(b)
的情况(一些(a),一些(b))=>一些(f(a,b))
情况(无,无)=>无
}

这种模式是否有名称?由于第一种情况和第二种情况,它并不完全是一个应用函数。随意使用Haskell或Scala代码进行回答。

它是 flatten + reduce

  List(a,b).flatten.reduceOption(f)
a ++ b reduceOption f //同样的结果


I wrote a Scala function:

  def liftOrIdentity[T](f: (T, T) => T) = (a: Option[T], b: Option[T]) =>
    (a, b) match {
      case (Some(a), None) => Some(a)
      case (None, Some(b)) => Some(b)
      case (Some(a), Some(b)) => Some(f(a, b))
      case (None, None) => None
    }

Is there a name for this pattern? It is not quite an applicative functor due to cases 1 and 2. Feel free to answer with Haskell or Scala code.

解决方案

On collection it is flatten + reduce:

List(a, b).flatten.reduceOption(f)
a ++ b reduceOption f // same result

这篇关于有没有这种解除功能的名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆