0

Trying to follow this example (taken from scala in action) for creating generic sum function for all collections

trait Summable[A] { def plus(a1: A, a2: A): A def init: A } object IntSummable extends Summable[Int] { def plus(a1: Int, a2: Int): Int = a1 + a2 def init: Int = 0 } trait Foldable[F[_]] { def foldLeft[A](xs: F[A], m: Summable[A]) : A } 

implementing foldLeft like this works fine :

 object ListFoldLeft extends Foldable[List] { def foldLeft[A](xs:List[A],m:Summable[A]) = xs.foldLeft(m.init)(m.plus) } 

however, however, although the foldLeft function accepts two parameters (xs:List[A],m:Summable[A]), we send them seperatly xs.foldLeft(m.init)(m.plus) and it works fine

but if I try to send them like this :

 object ListFoldLeft extends Foldable[List] { def foldLeft[A](xs:List[A],m:Summable[A]) = xs.foldLeft(m.init,m.plus) } 

I get

<console>:18: error: too many arguments for method foldLeft: (z: B)(op: (B, A) => B)B xs.foldLeft(m.init,m.plus) 

why ?

1 Answer 1

2

Maybe I'm wrong, but it looks like you try to call List . foldLeft[B](z: B)(f: (B, A) ⇒ B): B curried function with only one parameter list, and with invalid number of parameters in that defined parameter list.

List.foldLeft is a curried function

more about curried functions here:

http://www.codecommit.com/blog/scala/function-currying-in-scala

Sign up to request clarification or add additional context in comments.

6 Comments

I think you are missing my point : why do I get too many arguments when trying to implement it like this :object ListFoldLeft extends Foldable[List] { def foldLeft[A](xs:List[A],m:Summable[A]) = xs.foldLeft(m.init,m.plus) }
xs is a List and you call foldLeft with one parameter list and in that parameterlist with invalid number of parameters.
@igx I believe you are invoking List.foldLeft instead of ListFoldLeft.foldLeft on xs
@Daenyth is right, you invoke foldLeft on a different type than you would like to
because foldLeft is a curried function, foldLeft has two parameter lists with specified number of parameters and you try to use the first parameter list with invalid numebr of parameters. please check my original comment about curried functions
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.