I have implemented the following function:
/**
* Returns a function h , which is the composition of the functions f and g.
*/
def compose[A, B, C](g: B => C, f: A => B): A => C = f.andThen(g)
And I'm trying to test it with ScalaCheck. I could generate the following tests, which compile and pass:
import org.scalatest.prop.PropertyChecks
import org.scalatest.{FlatSpec, Matchers}
class ComposeSpec extends FlatSpec with Matchers with PropertyChecks {
"Compose" should "return a function h , which is the composition of the
functions f and g" in {
forAll { (a: Int, g: Int => Int, f: Int => Int) =>
compose(g, f)(a) should be(g(f(a)))
}
forAll { (a: String, g: Double => Int, f: String => Double) =>
compose(g, f)(a) should be(g(f(a)))
}
}
}
But, as you see, I'm generating arbitrary functions with defined types, and also matching the type of the parameter a
with the type of the input of the function f
. What I want to do would be something like this:
forAll { (a: A, g: B => C, f: A => B) =>
compose(g, f)(a) should be(g(f(a)))
}
But I don't know the syntax for that, nor if it's possible. Could you help me?