Function Syntax
A function object is an object that you can use like a function.In Scala, a function object is simply an object that has one or more apply methods.
object Square {
def apply(in: double) = in * in
}
Observations
1. To use the function object is also quite simple: val xSquared = Square(x)
2. Scala treats {object}({args}) as syntactic sugar for {object}.apply({args}).
Function Traits
All functions extend from a trait i.e scala.Function0 ... scala.Function9
where 0 to 9 are number of parameters that the function can take as parameter.
Explicit Definition of Functions using Function Traits
// Object with funtion which accepts one argument
object Square extends Function1[double,double] {
def apply(in: double) = in * in
}
// Object with funtion which accepts two argument
object Multiply extends Function2[double,double,double] {
def apply(in1: double,in2:double) = in1 * in2
}
The => notation
Function1[double,double] can be expressed as double => double.
An example of using these notations with an expression is denoted below.
object Square extends ((double) => double) {
def apply(in: double) = in * in
}
Closures
A Scala closure is simply a function object. It’s probably the most common way that function objects are used in Scala. In fact, Scala doesn’t even use the term closure—Scala calls it an anonymous function.
There is no difference in effect between statements mentioned below.
someMethod(Square)
someMethod{in => in * in}
someMethod{(in) => in * in}
someMethod(
new Function1[double, double]() {
def apply(in: double): double = in * in
}
)
ETA Extensions
Implicit conversion of a scala method to a scala function is called ETA Conversion.
Described below is a simple example which sites example on how ETA Conversion can be achieved.
object MethodCallWithETAExtensions {
def main(args: Array[String]) {
println("Result From square method" +someMethod(square))
println("Result From SquareWithFunctionExtension" +someMethod(SquareWithFunctionExtension))
println("Result From SquareWithSimplyFiedExtension" +someMethod(SquareWithSimplyFiedExtension))
}
// This method would be converted to a function def using the ETA Extension.
def square(in: Double) = in * in
// This method takes a function. Here were converting a Method to a funtion.
def someMethod(f: Double => Double) = f(12)
}
// This is a function Definition where we wouldnt need a method to a function conversion using ETA Extension
object SquareWithFunctionExtension extends Function1[Double,Double] {
def apply(in: Double) = in * in
}
// Another form of expressing function definiton
object SquareWithSimplyFiedExtension extends ((Double) => Double) {
def apply(in: Double) = in * in
}
1. The main method in MethodCallWithETAExtensions passes a method definition directly,someMethod which is expecting a funtion. This conversion of Method to a function implicitly is called ETA Conversion.
2. someMethod(SquareWithFunctionExtension) and someMethod(SquareWithSimplyFiedExtension) are both passing functions.
One call define a function by either extending Function1 trait or by using simpler form of ((parameter) => returnType)
Partial Functions
Functions with a limited domain are called partial functions.
The PartialFunction trait is a descendant of Function1 that adds a new method: isDefinedAt. This method can be used—and should be used—to determine if a proposed argument value is within the proper domain of the partial function or not. The behavior of the apply method is not defined when an improper argument value is provided—the function object can handle the situation however it pleases.
Example Code for Partial Functions.
object PartialFunctions {
var passedValue = 0;
def main(args: Array[String]) {
passedValue =12;
println(someMethod(SquarePartiual))
passedValue =4;
println(someMethod(SquarePartiual))
}
// This method takes a function. Here were converting a Method to a funtion.
def someMethod(f: PartialFunction[Double,Double]) = if(f.isDefinedAt(passedValue)) f(passedValue) else 50.0
}
// This is a function Definition where we wouldnt need a method to a function conversion using ETA Extension
object SquarePartiual extends PartialFunction[Double,Double] {
def apply(in: Double) = in * in
override def isDefinedAt(in : Double) = if (in > 4) true else false
}
How to run the above program.
1. Copy the program and paste it into a file named PartialFunctions .
2. Run >scalac PartialFunctions.scala
3. Run >scala PartialFunctions
4. Please see the example. Here isDefinedAt is determining if we need to call f(12) or just return some arbitrary value.
Factories
.. To be updated
I have tried to summarize all the information present in the blogs below.
Reproduction of the blogs is purely for posterity.
1 .Scala Functions From a Java Perspective
2. Scala Methods Vs Functions
3. partially-applied-functions

No comments:
Post a Comment