此代码:
def compose[A, B, C](f: B => C, g: A => B): A => C =
a => f(g(a)) //> compose: [A, B, C](f: B => C, g: A => B)A => C
def f(a : Int) = a + 1 //> f: (a: Int)Int
def g(a : Int) = a + 1 //> g: (a: Int)Int
compose[Int,Int,Int](f(1) , g(1))
导致编译器错误:
Multiple markers at this line - type mismatch; found : Int required: Int => Int - type mismatch; found : Int
required: Int => Int
但是由于f和g是接受Int并返回Int的函数,这与f: B => C
的函数定义不匹配,因为B& C将被输入Int?
答案 0 :(得分:4)
你应该撰写功能(你正在创作Ints)。 结果你得到另一个功能,即组合。 然后,您可以将参数传递给组合函数。
compose[Int,Int,Int](f , g)(1)
顺便说一下,Scala中有compose
(和andThen
)方法
(f _).compose(g)(1)
https://twitter.github.io/scala_school/pattern-matching-and-functional-composition.html