[英]Why are type declarations required in this Scala implementation of foldLeft
我曾尝试使用此代码在LinkedList上实现foldLeft,一个是curried foldLeft2,另一个是foldLeft:
sealed trait LinkedList[+E] {
@tailrec
final def foldLeft[A](accumulator: A, f: (A, E) => A): A = {
this match {
case Node(h, t) => {
val current = f(accumulator, h)
t.foldLeft(current, f)
}
case Empty => accumulator
}
}
@tailrec
final def foldLeft2[A](accumulator: A)(f: (A, E) => A): A = {
this match {
case Node(h, t) => {
val current = f(accumulator, h)
t.foldLeft2(current)(f)
}
case Empty => accumulator
}
}
}
但是当我使用foldLeft时,似乎我需要为accumulator和item声明类型,但对于foldLeft2,我没有。 有人可以解释为什么会这样吗?
class LinkedListSpecification extends Specification {
"linked list" should {
"foldLeft correctly" in {
val original = LinkedList(1,2,3,4)
original.foldLeft(0, (acc: Int, item: Int) => acc + item) === 10
}
}
"linked list" should {
"foldLeft2 correctly" in {
val original = LinkedList(1,2,3,4)
original.foldLeft2(0)((acc, item) => acc + item) === 10
}
}
}
这是因为Scala中的类型推断跨参数列表从左到右工作。
因此,在第二个版本foldLeft2
它能够在继续到下一个参数列表之前将类型A推断为Int
,其中它现在需要函数(Int,E)=>Int
。 在第一个版本foldLeft
它试图通过两个参数( accumulator
和f
)同时推断A
它抱怨你传递给它的匿名函数,因为它还没有推断出类型A
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.