tags:

views:

89

answers:

2

Update: detailed answer to the more general problem at http://stackoverflow.com/questions/1181533/what-are-the-precise-rules-for-when-you-can-omit-parenthesis-dots-braces-fu, thank you for the answers!

The following works:

scala> List(1,2,3) filter (_ > 1) reduceLeft(_ + _)
res65: Int = 5

And also the following:

scala> List(1,2,3).filter(_ > 1).foldLeft(0)(_ + _)
res67: Int = 5

But not this sytax:

scala> List(1,2,3) filter (_ > 1) foldLeft(0)(_ + _)
<console>:10: error: 0 of type Int(0) does not take parameters
       List(1,2,3) filter (_ > 1) foldLeft(0)(_ + _)
                                         ^                                           

What is a suggested fix?

+4  A: 

This works:

(List(1,2,3) filter (_ > 1) foldLeft 0) (_ + _)
Marimuthu Madasamy
+4  A: 

This topic is well described in http://stackoverflow.com/questions/1181533/what-are-the-precise-rules-for-when-you-can-omit-parenthesis-dots-braces-fu.

Curried functions seems to be little bit harder than methods with one parameter. To omit the dot, curried functions need to use parenthesis outside the infix call.

As Marimuthu Madasamy mentioned, this works (the object (List), the method (foldLeft) and its first parameter (0) are in parenthesis):

(List(1,2,3) filter (_ > 1) foldLeft 0) (_ + _)

Steve
Marimuthu and you should join your answers.
mkneissl