1

I am doing the Scala Coursera course, and lectures seem to be missing an explanation of how multiple parameter lists really work.

He makes claims like the averageDamp() function below could be called with just the first argument (f), allowing you to call it with the second later. It seems though that you need to do partial binding explicitly by following the call with "_".

However, if the partial binding call is being passed into another function that accepts a function with a signature matching the partially bound function, it will implicitly accept it, no "_" necessary.

Yet he doesn't use the term partial binding at all, just saying there is a special syntax in Scala for basically returning a closure, when in reality it is just partial binding. Or not?

scala> def averageDamp(f: Double => Double)(x: Double) = (x+f(x))/2

scala> def fixedPoint(f: Double => Double)(x: Int) = f(x)+1

scala> fixedPoint(averageDamp(x=>x+1))(2)
res29: Double = 3.5

scala> averageDamp(x=>x+1)
<console>:19: error: missing arguments for method averageDamp;
follow this method with `_' if you want to treat it as a partially applied function
       averageDamp(x=>x+1)

A non-partial binding version of averageDamp might be like:

def averageDamp(f: Double => Double): (Double => Double) = 
    def inner(x: Double): Double =
        (x+f(x))/2    
    inner

I guess my question is...is the multi-parameter list version of averageDamp() being passed into another function just implicit partial binding...or is this really some kind of special Scala syntax for returning an inner function/closure?

1
  • 1
    For the record, a lot of questions like these can be answered by scala -print (although you'll have to dig through the repl-related code). You can see that def foo(a: Int)(b: Int): Int gets compiled to def foo(a: Int, b: Int): Int for performance reasons. Commented Jun 15, 2016 at 21:44

1 Answer 1

1

Yet he doesn't use the term partial binding at all, just saying there is a special syntax in Scala for basically returning a closure, when in reality it is just partial binding... is the multi-parameter list version of averageDamp() being passed into another function just implicit partial binding...or is this really some kind of special Scala syntax for returning an inner function/closure?

Why do you think it's one or the other? Supplying only some of parameter lists is a special syntax for partial application (not "binding", usually) of methods with multiple parameter lists.

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

2 Comments

"Supplying only some of parameter lists is a special syntax for partial application" Except when I do that it results in an error like "missing arguments for method averageDamp; follow this method with `_' if you want to treat it as a partially applied function".
Yes, _ is part of this syntax (it is allowed to be omitted if the expected type is a function type).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.