为什么在foldLeft中留下点会导致编译错误?

编程入门 行业动态 更新时间:2024-10-18 20:21:20
本文介绍了为什么在foldLeft中留下点会导致编译错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

任何人都可以解释为什么我忽略用于应用 foldLeft 函数的点符号(版本2.9.2)时为什么会看到以下编译错误

Can anyone explain why I see this compile error for the following when I omit the dot notation for applying the foldLeft function?(version 2.9.2)

scala> val l = List(1, 2, 3) res19: List[Int] = List(1 ,2 ,3) scala> l foldLeft(1)(_ * _) <console>:9: error: Int(1) does not take parameters l foldLeft(1)(_ * _) ^

但是

but

scala> l.foldLeft(1)(_ * _) res27: Int = 6

不适用于其他高阶函数,如 map ,它似乎并不关心我是否提供点。

This doesn't hold true for other higher order functions such as map which doesn't seem to care whether I supply the dot or not.

我不认为它是一个关联事物,因为我不能只调用 foldLeft(1)

I don't think its an associativity thing because I can't just invoke foldLeft(1)

推荐答案

这是因为foldLeft是咖喱。除了使用点符号,还可以通过添加圆括号来解决此问题:

It's because foldLeft is curried. As well as using the dot notation, you can also fix this by adding parentheses:

scala> (l foldLeft 1)(_ * _) res3: Int = 6

并且关于您对无法调用 foldLeft(l)的评论,您可以,但您需要像这样部分应用它:

Oh - and regarding your comment about not being able to invoke foldLeft(l), you can, but you need to partially apply it like this:

scala> (l foldLeft 1) _ res3: ((Int, Int) => Int) => Int = <function1>

更多推荐

为什么在foldLeft中留下点会导致编译错误?

本文发布于:2023-11-09 20:57:51,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   foldLeft

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!