简体   繁体   English

发送函数以在Scala中折叠

[英]Sending functions to fold in Scala

How do I send an accumulation function to fold in Scala, the below example will say (Int,Int,Int) does not take parameters . 我如何发送一个累加函数在Scala中折叠,下面的示例将说(Int,Int,Int) does not take parameters

My questions are: 我的问题是:

  • Why do I get this error? 为什么会出现此错误?
  • How is the idomatic way to do what I want in the code below? 在下面的代码中,我如何使用idomatic方法进行操作?

     def a(xs: List[(Int,Int)]): Int = { def logic(acc: (Int,Int, Int), post: (Int,Int)): (Int,Int,Int) = { // do some logic (1, 2, 3) } val to = xs foldLeft((0,0,0))(logic _) to._3 } 

The problem is here: 问题在这里:

xs foldLeft((0,0,0))(logic _)

Never go for the dotless notation unless it's for an operator. 除非适用于运算符,否则切勿使用无点表示法。 This way it works. 这样工作。

xs.foldLeft((0,0,0))(logic _)

Without a context I believe this is idiomatic enough. 没有上下文,我相信这已经足够了。

我没有您代码的目标,但您可以通过以下方式解决您描述的问题:

val to = xs.foldLeft((0,0,0))(logic)

尝试这个

xs.foldLeft((0,0,0), logic)

foldLeft is a curried function so it requires a . foldLeft是咖喱函数,因此需要一个. - go for xs.foldLeft and it will work. -选择xs.foldLeft ,它将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM