scalahaskellfunctional-programminglifting

Is there a name for this kind of lifting a function?


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.


Solution

  • On collection it is flatten + reduce:

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