Reputation: 7394
How do I send an accumulation function to fold in Scala, the below example will say (Int,Int,Int) does not take parameters
.
My questions are:
How is the idomatic way to do what I want in the code below?
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
}
Upvotes: 0
Views: 68
Reputation: 853
foldLeft
is a curried function so it requires a .
- go for xs.foldLeft
and it will work.
Upvotes: 1
Reputation: 3525
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.
Upvotes: 2
Reputation: 8866
I don't get the goal of your code but the problem you described can be solved this way:
val to = xs.foldLeft((0,0,0))(logic)
Upvotes: 1