squall
squall

Reputation: 139

How to make a product of function in Scala

I've got an exercise to create a product of two functions from the code below:

 def prod[A, B, C, D](f: A => C, g: B => D): (A, B) => (C, D) = {
    
  }

My plan was to do something like this, but it doesn't work, because it can't resolve symbols A and B.

def prod[A, B, C, D](f: A => C, g: B => D): (A, B) => (C, D) = {
        v1: (A,B) => f(A)*g(B)
      }

Upvotes: 0

Views: 145

Answers (1)

SwiftMango
SwiftMango

Reputation: 15284

The syntax to define a function type signature and a function definition is different. When you return a value, you need to return the function definition.

def prod[A, B, C, D](f: A => C, g: B => D): (A, B) => (C, D) = 
  (a: A, b: B) => (f(a), g(b))

Upvotes: 2

Related Questions