The Artima Developer Community
Sponsored Link

Programming in Scala Forum
Partially applied function (8.6, page 182)

4 replies on 1 page. Most recent reply: Dec 4, 2008 5:32 AM by Vladimir Kelman

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 4 replies on 1 page
Vladimir Kelman

Posts: 46
Nickname: vkelman
Registered: Feb, 2008

Partially applied function (8.6, page 182) Posted: Nov 20, 2008 10:19 PM
Reply to this message Reply
Advertisement
On a page 179 you defined

val someNumbers = List(-11, -10, -5, 0, 5, 10)

Theb, on a page 182 you said:

someNumbers.foreach(println _)

...

When you use an underscore in this way, you are writing a partially applied function... A partially applied function is an expression in which you don't supply all of the arguments needed by the function... Here's an example:
val a = sum _

I believe that foreach() code above is not a correct example or, at least, is not good example of the partially applied function. I think you contradict to yourself, because someNumbers.foreach(println _) DOES supply a required argument to println function. So you fully apply arguments here - it not a partially applied function.

The following examples of
val a = sum _
val b = sum(1, _: Int, 3)
really demonstrate partially applied functions, because noty all the required parameters are specified in the right expressions.


Vladimir Kelman

Posts: 46
Nickname: vkelman
Registered: Feb, 2008

Re: Partially applied function (8.6, page 182) Posted: Nov 23, 2008 1:58 PM
Reply to this message Reply
Or maybe it makes sense to say that in someNumbers.foreach(println _), a "println _" expression defines a partially applied function on which a temporary function value instance is immediately created and applied - all this happens during first phase of someNumbers.foreach() method invocation, when parameters of foreach() method are evaluated.

There should be some kind of explanations added,like my attempt above, because in a current form it is confusing.

j herber

Posts: 6
Nickname: 53144
Registered: Jan, 2008

Re: Partially applied function (8.6, page 182) Posted: Nov 23, 2008 3:09 PM
Reply to this message Reply
that's shorthand for function literals (anonymous functions in scala) with placeholders.

so,

someNumbers.foreach( x => println(x) )

and

someNumbers.foreach( println(_) )

and

someNumbers.foreach(println _ )

and

someNumbers.foreach(println)

are all equivalent forms of the first form.

see section 8.6 for the "why's".

Vladimir Kelman

Posts: 46
Nickname: vkelman
Registered: Feb, 2008

Re: Partially applied function (8.6, page 181) Posted: Nov 23, 2008 4:00 PM
Reply to this message Reply
Yes, I understand that (my previous comment was wrong).

But my point is: yes, it is a function literal with placeholders, but there is no partially applied function here, because the only argument of this function literal is supplied at the point of invocation.

Again, a book says on a page 181 (in last edition) that, "In Scala, when you invoke a function, passing in any needed arguments, you apply that function to the arguments... A partially applied function is an expression in which you don't supply all of the arguments needed by the function. Instead, you supply some, or none, of the needed arguments."
But in our case we never invoke our function literal without supplying its single argument, so there is no partially appllied function there.

All book examples shows how to create partially applied functions from regular functions ( def sum(a: Int, b: Int, c: Int) = a + b + c, defined on a page 181 ), not from function literals. I'm not sure if it is possible and makes sense to create a partially applied function from function literal.

Vladimir Kelman

Posts: 46
Nickname: vkelman
Registered: Feb, 2008

Re: Partially applied function (8.6, page 181) Posted: Dec 4, 2008 5:32 AM
Reply to this message Reply
I got comprehensive answer here.

Flat View: This topic has 4 replies on 1 page
Topic: Scala is not friendly with space, and if-without-else Previous Topic   Next Topic Topic: How does The Book sell so far?

Sponsored Links



Google
  Web Artima.com   

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