Tag: jvm

This blog lists key points I noticed while trying to shift gears to Functional Programming using Scala JVM language coming from a traditional Object-oriented programming background for over a decade. I’ve extensively used Coursera’s Scala course and online documentation/blogs to come-up with this content.

What is Functional programming

Mainstream languages like C, C++, Java etc were based on the idea of update in place. They presuppose that the way to solve a programming problem is to have procedures that read and write values to memory locations, be it directly visible to the programmer (by means of pointers) or not.

Other languages, like Lisp, are based on a different assumption: a computation is best expressed as a set of functions over immutable values. So, in order to produce any kind of repetitive computation, recursion is essential. This assumption has strong roots in lambda calculus and related mathematical formalisms.

Function evaluation strategies

Scala function can be evaluated either Call-by-name or Call-by-value. Call-by-value has the advantage that it evaluates every function argument only once. Call-by-name has the advantage that a function argument is not evaluated if the corresponding parameter is unused in the evaluation of the function body.

Scala uses Call-by-value generally, unless the type of function parameter starts with ‘=>’ in which Call-by-name is used.

Tail-recursion

Recursion has been looked-down upon in Imperative style programming but its the basic building block for Functional Programming. Stack-overflow issue still exists if you have too many recursive calls and thats where Tail-recursion comes into play. We can convert any Recursive function into Tail-recursion by using an intermediate function containing an Aggregator. This lets Compiler use the same stack-space for all recursive method calls instead of creating a new one for each Recursive call. Below is an example of factorial definition.

Higher-order functions and Currying

Functions are first-class citizens which gets treated just like any primitive types, i.e. you can pass it as a parameter to a function, return it from a function and define a var to hold a function.

Currying is when you break down a function that takes multiple arguments into a series of functions that take part of the arguments. Advantage of this is that it offers great amount of expressiveness and reusability to a function. Function arguments associate to the left in context of Currying. Basically, this lets you express your solution just as if you you write a mathematical equation.

We’ll develop motivation for Currying step-by-step:

1

2

3

4

5

6

7

def sum(f:Int=>Int,a:Int,b:Int)={

def loop(a:Int,acc:Int):Int=

if(a>b)acc

elseloop(a+1,f(a)+acc)

loop(a,0)

}

sum(x=>x *x,3,5)

1

2

3

4

5

def sumInts(a:Int,b:Int)=sum(x=>x,a,b)

def sumCubes(a:Int,b:Int)=sum(x=>x*x*x,a,b)

def sumFactorials(a:Int,b:Int)=sum(fact,a,b)

Since a and b are getting passed unchanged from sumInts and sumCubes into sum, we can get their definitions even shorter by getting rid of params as follows:

1

2

3

4

5

6

def sum(f:Int=>Int):(Int,Int)=>Int={

def sumF(a:Int,b:Int):Int=

if(a>b)0

elsef(a)+sumF(a+1,b)

sumF

}

Here sum is now a function that returns another function. Returned functions sumF applies the given function parameter f and sums the results. We can now define earlier 3 functions as follows:

1

2

3

4

5

def sumInts=sum(x=>x)

def sumCubes=sum(x=>x*x*x)

def sumFactorials=sum(fact)

These functions can in turn be applied like any other function:

1

sumCubes(1,10)+sumFactorials(10,20)

In above example, we can avoid sumInts, sumCubes & sumFactorials middlemen as follows:

sum (cube) (1,10)

sum(cube) applies sum to cube and returns ‘sum of cubes’ function

sum(cube) is therefore equivalent to sumCubes

This function is next applied to arguments (1,10)

Generally, function application associates to the left:

sum (cube)(1,10) == (sum (cube)) (1,10)

The definition of functions that return functions is so useful in functional programming that there is a special syntax for it in Scala. For example, the following definition of sum is equivalent to the one with the nested sumF function, but shorter:

1

2

3

def sum(f:Int=>Int)(a:Int,b:Int):Int=

if(a>b)0elsef(a)+sum(f)(a+1,b)

Thus Currying resembles the process of evaluating a function of multiple variables, when done by hand, on paper as follows:

For example, given the function f(x,y)=y/x

To evaluate f(2,3), first replace x with 2

Since the result is a function of y, this new function g(y) can be defined as g(y)=f(2,y)=y/2

Next, replace the y argument with 3, producing g(3)=f(2,3)=3/2

On paper, using classical notation, this is usually done all in one step. However, each argument can be replaced sequentially as well. Each replacement results in a function taking exactly one argument. Below is an example where we can use wildcard as a placeholder for unknown param that’ll be provided later:

1

2

3

4

5

6

7

8

scala&gt;def concat(w1:String)(w2:String)=w1+" "+w2

concat:(w1:String)(w2:String)String

scala&gt;val hello=concat("hello")_

hello:String=&gt;String=

scala&gt;hello("world")

res0:String=hello world

Example: MapReduce

Below is screenshot of IDE where product function is implemented with Currying principles. mapReduce generalizes this further more by supporting any operation like sum, product etc.

With above context, we can re-implement product in terms of mapReduce as follows:

We are not instantiating Trait directly, but rather creating a suitable object for the Trait to attach itself to so you can use the Trait’s functionality without needing to define a class that extends the Trait.

“AnyRef” is just alias of java.lang.Object and is the base class of all reference types

“AnyVal” is base type of all primitive types

“Nothing” is sub-type of every other type. It’s used to signal abnormal termination (for eg. ‘throw Exc’ expressionn is used to abort evaluation and its type is Nothing) and, also as element type of empty collections as in Set[Nothing]

“Null” is subtype of every class/ref inherited from Object/Any thus incompatible with AnyVal. Every reference class type also has null as a value.

As in Java, Polymorphism is achieved by both sub-classing and generics. Below is an example:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

traitList[T]{

def isEmpty:Boolean

def head:T

def tail:Lis[T]

}

classCons[T](val head:T,val tail:List[T])extendsList[T]{

def isEmpty=false

}

classNil[T]extendsList[T]{

defisEmpty=true

def head=thrownewNoSuchElementException("Nil.head")

def tail=thrownewNoSuchElementException("Nil.tail")

}

def singleton[T](elem:T)=newCons[T](elem,newNil[T])

Note that:

Type parameters doesn’t effect Scala during runtime as it uses Type-erasure(type info is used only by compiler and not preserved for runtime)

If we define a method param as val, it implicitly defines a val with same name in method definitionand assigns method param to it.

Generics in methods: singleton method can be invoked as follows with the type info:

1

2

singleton[Int](1)

singleton[Boolean](true)

Alternatively, we can let Scala deduce type info from the context thus letting us invoke singleton as follows:

1

2

singleton(1)

singleton(true)

Above code creates what is called as Immutable linked list. This is fundamental to Functional programming which is constructed using 2 basic elements:
Nil: the empty list
Cons: cell containing an element and remainder of listBelow is graphical representation of Immutable linked lists:

Functions as Objects

Function-types relates to Classes and function value relate to objects. Functions type A => B is just abbreviation for scala.Function1[A,B] which is define as follows:

1

2

3

4

packagescala

trait Function1[A,B]{

def apply(x:A):B

}

There are also traits with Function2, Function3 etc to support upto 22 params.

Anonymous function such as (x: Int) => x * x would be expanded as follows:

1

2

3

4

classAnonFun extendsFunction1[Int,Int]{

def apply(x:Int)=x *x

}

newAnonFun

This in turn gets converted to as follows using Java anonymous class syntax:

1

2

3

newFunction1[Int,Int]{

def apply(x:Int)=x*x

}

Function calls such as f(a,b) is expanded to f.apply(a,b). For eg:

1

2

valf=(x:Int)=>x*x

f(7)

would be translated to:

1

2

3

4

valf=newFunction1[Int,Int]{

def apply(x:Int)=x*x

}

f.apply(7)

ETA Expansion

Note that method such as def f(x: Int): Boolean = ... is not a function value as it ends in infinite expansion. But when name of method is used in place where function type is expected, then its converted to function value (x: Int) => f(x) which gets expanded as below. This is known as ETA expansion :

In summary, functions and methods are not the same in Scala (although most of the cases we can ignore this difference). A Scala method, as in Java, is a part of class whereas Function is a complete object (instance of class that implement one the Traits Function0,Function1,Function3..)

Pattern matching

Pattern matching provides a functional way of object decomposition. A case class definition adds feature to pattern match on classes. This modifier adds the benefits of

‘==’: does a structural equality comparison, i.e two case class instances with same params are equal

toString: method that gives string representation of class members

copy(): we can copy whole of a case class instance values into a new instance.

all params will be declared with ‘val’ by default. Note that if you use currying fashion, only the first param(s) will be provided with ‘val’

Since the concrete sub-class has no body, we will be using Pattern matching using ‘match’ keyword.

For eg,

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

trait Expr

//declare case classes

caseclassNumber(n:Int)extendsExpr

caseclassSum(e1:Expr,e2:Expr)extendsExpr

//This internally declares Number and Sum singleton objects that

//lets us simply invoke Number(2) and Sum(2,3)

// object Number { def apply(n: Int) = new Number(n) }

// object Sum { def apply(e1: Expr, e2: Expr) = new Sum(e1,e2)

//provide implementation for case classes

def eval(e:Expr):Int=ematch{

caseNumber(n)=>n

caseSum(e1,e2)=>eval(e1)+eval(e2)

}

Note match statement execution is sequential as it executes all cases starting from top to bottom one-by-one and skips after a matching case.

A good side-effect of such a matching is that it provides an alternative way to design functions. For example, consider the factorial function. If you choose the recursive version, usually, you would define it like this:

1

2

3

def fact(n:Int):Int=

if(n==0)1

elsen *fact(n-1)

Where-as a Pattern matching based solution is:

1

2

3

4

def fact(n:Int):Int=nmatch{

case0=>1

casen=>n *fact(n-1)

}

Lists

Lists are immutable and are recursive whereas Arrays are mutable and flat.

val l = List(1,2,3) is similar to using Cons (short form for constructor) operator as 1::2::3::Nil.
Note that operators ending in ‘:’ are right associative and thus above can be translated as Nil.::3.::2.::1

All operations on lists can be expressed in terms of ‘head’, ‘tail’ and ‘isEmpty’

Lists can also be used in pattern matching as follows:1 :: 2 :: xs Lists starting with 1 and then 2x :: Nil or List(x) Lists with length 1List() or Nil Empty listList(2 :: xs) List containing only element as another list that start with 2

Below is an example of a typical way to decompose lists with pattern-matching using Insertion sort as an example:

1

2

3

4

def isort(xs:List[Int]):List[Int]=xsmatch{

caseList()=>List()//check if list is empty

casey::ys=>insert(y,isort(ys))//if not empty, decompose list into its head and tail and then process

When is a List preferred to a Vector ?

List: if our operations involve having a Head and Tail of a sequence as these are done at constant-time with Lists whereas its complicated with a Vector

Vector: when we need bulk operations like Map, Filter, Fold

Vectors are created analogous to Lists including its operations. Only exception is we’ll replace :: with :+. x +: xs creates new Vector with leading element x followed by elements of xs and +: creates new Vector with trailing element x preceded by all elements of xs.