disshethard
disshethard

Reputation: 83

How to define a lift function in Scala

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 = /* ... */

Upvotes: 3

Views: 73

Answers (1)

Mario Galic
Mario Galic

Reputation: 48430

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

Upvotes: 2

Related Questions