« Return to Thread: Partially applied function error - OK in foldLeft

Re: Partially applied function error - OK in foldLeft

by Mario Camou :: Rate this Message:

Reply to Author | View in Thread

Hi Jem,

seed(1)=z means "call function z and assign the return value". The principle of uniform access means that Scala doesn't require parentheses on method calls with no arguments. To get around this you use the underscore to indicate you want to rederence the function object itself.

Hope this helps,
-Mario.

- Sent from my Android G1

On Jul 7, 2009 9:31 AM, "Jem" <jem.mawson@...> wrote:

Hi. I'm not clear on why these two scenarios are different. Can someone explain why the foldLeft code works, but the map(i)=z operation fails?


scala> def z(s: String) = s
z: (String)String

scala> val seed = Map.empty[Int, (String)=>String]    
seed: scala.collection.immutable.Map[Int,(String) => String] = Map()

scala> List(1,2,3).foldLeft(seed) {(acc, next) => acc(next) = z}                            
res23: scala.collection.immutable.Map[Int,(String) => String] = Map(1 -> <function>, 2 -> <function>, 3 -> <function>)


scala> seed(1) = z
<console>:7: error: missing arguments for method z in object $iw;
follow this method with `_' if you want to treat it as a partially applied function
       seed(1) = z
                 ^


I know z _ will work, as per the message, but I don't understand how the latter operation differs from the foldLeft scenario. In both cases I invoke map(int)=<function>. What is the difference?

Thanks
Jeremy

 « Return to Thread: Partially applied function error - OK in foldLeft