How to define a lift function in Scala

Viewed 56

Can someone help me, how to define this function:

def lift[A, B, T](op: (T,T) => T)(f: A => T, g: B => T): (A,B) => T = /* ... */
1 Answers

Perhaps

def lift[A, B, T](op: (T,T) => T)(f: A => T, g: B => T): (A,B) => T =
  (a: A, b: B) => op(f(a), g(b))

which gives

def op(a: Int, b: Int): Int = a + b
def f(x: String): Int = x.toInt
def g(x: List[String]): Int = x.length

lift(op)(f,g)("41", List("2"))
// Int = 42
Related