The Artima Developer Community
Sponsored Link

Programming in Scala Forum
Partially applied functions

2 replies on 1 page. Most recent reply: Nov 27, 2016 7:40 AM by Daniel Barclay

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Mohamed Alla Pitchai

Posts: 1
Nickname: morabeek
Registered: May, 2016

Partially applied functions Posted: May 29, 2016 12:51 PM
Reply to this message Reply
Advertisement
Hello,
On page 151 (second edition) there is a statement in the first paragraph which states the following:
"Although you can't assign a method or nested function to a variable, or pass it an argument to another function, you can do these things if you wrap the method or nested function in a function value by placing an underscore after its name."

I am not sure why I can't send a function as an argument to another function. I don't understand the author's point. Please give an example. Please consider the following example.


object PartiallyAppliedFunction {


def square(x:Int) = x * x

def sumOfSquares(x:Int=>Int, a:Int, b:Int) = x(a) + x(b)

def main(args:Array[String]) {
println("hello world");

println(sumOfSquares(square, 1,2))
}

}


In the above example I am passing "square" function to "sumOfSquares" function. So I am thinking why it is not possible to pass a method as argument to another function.
Please help me understanding the statement given in the paragraph mentioned at the beginning.

Thanks & Regards,
Mohamed


Jingjing Duan

Posts: 1
Nickname: jduan
Registered: May, 2016

Re: Partially applied functions Posted: May 29, 2016 10:40 PM
Reply to this message Reply
What the author meant was you can't assign a function to a var or val. See line 10 below.

It seems you can pass a function to another function. However, that's only a "special case". A few pages later, the author said "If you are writing a partially applied function expression in which you leave off all parameters, such as println _ or sum _, you can express it more concisely by leaving off the underscore if a function is required at that point in the code". In other words, when you pass a function as a partially applied function to another function, you can pass it as is. (again, this is only a compiler trick, you are actually still passing square _, see line 17 below)

To be honest, I think this is kind of lame. In almost all functional programming languages, you can assign functions to vals or vars without the ugly trailing underscore.


1 object PartiallyAppliedFunction {
2 def square(x:Int) = x * x
3
4 def sumOfSquares(x:Int=>Int, a:Int, b:Int) = x(a) + x(b)
5
6 def main(args:Array[String]) {
7 println("hello world");
8
9 // this line won't compile
10 // val squareAlias = square
11
12 // this line will compile
13 val squareAlias = square _
14
15 println("square of 3 is: " + squareAlias(3))
16
17 println(sumOfSquares(square _, 1,2))
18 }
19
20 }

Daniel Barclay

Posts: 5
Nickname: daniel7
Registered: Sep, 2016

Re: Partially applied functions Posted: Nov 27, 2016 7:40 AM
Reply to this message Reply
> you can't assign a function to a var or val.

Did you mean that you can't assign a method to a var or val?


> val squareAlias = ...

In that declaration and initialization, what is being assigned to squareAlias other than a function (a references to a function object)?

Flat View: This topic has 2 replies on 1 page
Topic: (0).to(2) vs 0.to(2) Previous Topic   Next Topic Topic: Why Func literals require type casting when overloading is used

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use