stmfunk stmfunk - 3 months ago 45
Scala Question

Function Composition in Scala

I am trying to write a function that takes two functions and composes them. I tried the solution below and a few other variants but not much success.

def compose1[A, B, C](f: A => B, g: B => C)(x: A) = f(g(x))

The error I get is

<console>:21: error: type mismatch;
found : x.type (with underlying type A)
required: B
def compose1[A, B, C](f: A => B, g: B => C)(x: A) = f(g(x))

Full disclosure this is part of a university exercise designed to teach us Scala although it's not for credit. The question is really poorly phrased (it was literally the function declaration and the sentence I used above) and there was no examples or explanation given before hand, I think I don't have a good enough understanding of Scala to even understand the question. Would greatly appreciate some elaboration on the problem, thanks for help!


Look closely at the types: you are trying to feed x (type A) to the function g (which takes a B and returns a C). I'm sure you can pick it up from here.