Partially applied functions in Scala
Before going into Partially applied Functions, let me introduces 2 terms-
Function Literal- This exists in the source code- Something similar to Class definition. So we have
(x:Int,y:Int) => x+y
Function value- When this function literal is assigned to a reference they become function values. The are similar to the Objects created from the class. These are created at runtime
var sum = (x:Int,y:Int) => x+y
So in the above case we can use sum to invoke the method.
println(sum(5,7))
So coming to the partially applied functions- We always pass all the arguments while invoking the method. But in Scala we can pass some of the arguments defined in the function there by creating a partially applied function. So the rest of the arguments can be passed when the function is being invoked. Lets have a look at an example before going into detail of how its implemented.
object PartialFunction extends Application
{
def sum(a:Int,b:Int,c:Int) = a+b+c
val partial1 = sum _
val partial2 = sum(1,_:Int,8)
println("Partial 1: "+partial1(5,6,7))
println("Partial 2: "+partial2(4))
}
The output would be:
C:\dev\scala\partial>fsc PartialFunction.scala C:\dev\scala\partial>scala PartialFunction Partial 1: 18 Partial 2: 13
Lets pay attention to the line-4 and line-5 above. With
val partial1 = sum _
we say that the arguments would be supplied later on, when its invoked using partial1. With
val partial2 = sum(1,_:Int,8)
we are providing 2 arguments now and the third one would be provided when invoked using partial2. In each of the above 2 line- A function value object is created which is of type- Function3 (for partial1) and Function1(for partial2). FunctionN is a trait with an apply method with N parameters. So Function1 means- an apply method with 1 parameter and so on. Now that we have the function values- we invoke them by passing required arguments.
Lets look what methods does the class file generate-
C:\dev\scala\partial>javap PartialFunction
Compiled from "PartialFunction.scala"
public final class PartialFunction extends java.lang.Object{
public static final void main(java.lang.String[]);
public static final void scala$Application$_setter_$executionStart_$eq(long);
public static final long executionStart();
public static final scala.Function1 partial2();
public static final scala.Function3 partial1();
public static final int sum(int, int, int);
}
So we can see 2 methods there- partial2() and partial1(). We might be surprised to see that they arent taking any arguments. But in our example above we have passed arguments to these function values. So there’s something wrong?? No, there’s nothing wrong. If we look at the return type for partial1 and partial2 its Function3 and Function1 respectively. And when you compile the scala code shown in the beginning, you get 2 more class files generated:
C:\dev\scala\partial>ls PartialFunction$$anonfun$1.class PartialFunction$$anonfun$2.class
And when inspected these classes, the following could be seen
C:\dev\scala\partial>javap PartialFunction$$anonfun$1
Compiled from "PartialFunction.scala"
public final class PartialFunction$$anonfun$1 extends scala.runtime.AbstractFunction1$mcII$sp implements java.io.Serializable{
public static final long serialVersionUID;
public static {};
public final int apply(int);
public int apply$mcII$sp(int);
public final java.lang.Object apply(java.lang.Object);
public PartialFunction$$anonfun$1();
}
You can see that class extends scala.runtime.AbstractFunction1(this actually extends Function1). So PartialFunction$$anonfun$1 IS-A Function1. You can also see an apply method with one argument- the missing argument in the function value. So we are actually invoking this apply method. Same goes with the other class as well:
C:\dev\scala\partial>javap PartialFunction$$anonfun$2
Compiled from "PartialFunction.scala"
public final class PartialFunction$$anonfun$2 extends scala.runtime.AbstractFunction3 implements java.io.Serializable{
public static final long serialVersionUID;
public static {};
public final int apply(int, int, int);
public final java.lang.Object apply(java.lang.Object, java.lang.Object, java.lang.Object);
public PartialFunction$$anonfun$2();
}
Note that the above apply() takes three arguments/complete argument list.This indicates that the function(partial1) doesnt not take any arguments when creating a function value of sum function.
I had asked a query on Stackoverflow regarding the advantages of using Partially Applied functions. So this is what I could get from the replies there:
Although you can’t assign a method or nested function to a variable, or pass it as 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.
No related posts.
3 Responses to Partially applied functions in Scala
Connect to us …
Archives
- May 2013 (7)
- April 2013 (6)
- March 2013 (6)
- January 2013 (5)
- November 2012 (2)
- September 2012 (1)
- July 2012 (5)
- June 2012 (1)
- May 2012 (4)
- April 2012 (7)
- March 2012 (2)
- February 2012 (4)
- December 2011 (2)
- November 2011 (4)
- October 2011 (2)
- September 2011 (1)
- August 2011 (1)
- July 2011 (1)
- June 2011 (1)
- May 2011 (1)
- April 2011 (1)
- February 2011 (4)
- December 2010 (3)
- November 2010 (2)
- September 2010 (2)
- August 2010 (3)
- May 2010 (2)
- March 2010 (6)
- December 2009 (1)
- November 2009 (3)
- July 2009 (6)
- June 2009 (3)
- May 2009 (1)
- April 2009 (6)
- March 2009 (1)
- January 2009 (1)
- December 2008 (8)
- November 2008 (5)
- October 2008 (6)
- September 2008 (4)
- August 2008 (8)
- July 2008 (19)
- June 2008 (29)
- May 2008 (27)
- April 2008 (11)
- March 2008 (8)
- February 2008 (22)
- January 2008 (3)
Send To Readmill
Send to ReadmillRecent Posts
- Book review: The Object-Oriented Thought Process
- How to show links in ADF Messages
- Runtime Polymorphism in Java
- Understanding RowKey values in ADF TreeTable
- Train Wreck Pattern – A much improved implementation in Java 8
- Converting a List into comma separated value string in Java
- A simple application of Lambda Expressions in Java 8
- Template Method Pattern- Using Lambda Expressions, Default Methods
- First look at Learning Play! Framework 2
- Book review: Confessions of a Public Speaker
Disclaimer
Some of the links contained within this site have my referral id, which provides me with a small commission for each sale. Thank you for your support.










What is this language Scala dude ? Can you give us a brief overview ?
Thanks
Javin
Why String is immutable in Java
Scala is a programming language which runs on JVM just like Java- but it is based on the Functional and Object oriented paradigm. Even I have been learning this from a very long tie- difficult to get pick the Functional programming style. You can visit http://www.scala-lang.org for more information.
[...] Functions before diving into understanding this method. Partial Functions are different from the partially applied functions. Partial Functions are those functions which are defined for certain parameter values from within [...]