This document provides an overview of functional programming concepts in Scala. It discusses the history and advantages of functional programming. It then covers the basics of Scala including its support for object oriented and functional programming. Key functional programming aspects of Scala like immutable data, higher order functions, and implicit parameters are explained with examples.
5. Programming Paradigms
● Programming Paradigm is a approach to programming
● It will be based on set of principles or theory
● Different ways of thinking.
● Different types
○ Imperative
○ Functional
8. Expression vs Statement
Expression : evaluated to produce a value without changing state.
Statement : executed to update state.
Example:
Expression -> val isEven = if(x%2 == 0) true else false
11. History of Functional Programming
● Mathematical abstraction Lambda Calculus was the foundation
for functional Programming
● Lambda calculus was developed in 1930s to be used for
functional definition, functional application and recursion.
● It states that any computation can be achieved by sending the
input to some black box, which produces its result.
● This black box is lambda functions, and in FP it’s functions.
● LISP was the first functional programming language. It was
defined by McCarthy in 1958
12. Imperative vs Functional Programming
Characteristics Imperative Functional
State changes Important Non-existent
Order of execution Important Low importance
Primary flow control
Loops, conditions and method
(function) calls
Function calls
Primary manipulation unit
Instances of structures or
classes
Functions
13. Characteristics of FP
● Immutable data
● Lazy evaluation
● No side effects
● Referential transparency
● Functions are first class citizens
14. Immutable data
Mutable Immutable
var collection = [1,2,3,4]
for ( i = 0; i<collection.length; i++) {
collection[i]+=1;
}
Uses loop for updating
collection is updated in place
val collection = [1,2,3,4]
val newCollection = collection.map(value
=> value +1)
Uses transformation for change
Creates a new copy of collection. Leaves
collection intact
15. Advantages of FP
● Better modularity
● Shorter code
● Increased developer productivity
● Less number of bugs
● Perfect for distributed computing because of immutability
● Easier debugging
16. Limitations of FP
● Real world programs have to do side effects
● Difficult to understand for the beginner
● Very difficult to debug the nature of lazy evaluation
● Memory intensive
18. Scala History
● Martin Odersky is the creator
● Curious about compilers from beginning
● Created first compiler in 1995 - Pizza
○ Generics,Function pointers, Case class and pattern matching
● Led to javac compiler and generics eventually
○ Java is a strict language and hard to implement these new ideas
19. Scala History cont..
● 1999 - Started working on a language
○ Combines Object orientation with functional programming
○ Created Funnel language
○ It was not a user friendly language
○ New languages with less libraries are hard to adopt
● In 2001, design for Scala started, released first in 2003
● Scala is not an extension of Java
● Scala translates into JAVA byte code
● One implementation of .Net compatible version, now out dated
20. Scala Philosophy
● Growable language
○ Embed new DSL/add new types
● Scalable language
○ Same concepts in small and large applications
● Deep rather than broad
○ Focus on abstraction and composition
21. Why scala
● Hybrid programming language
● Statically typed
● Runs on the jvm
● Can execute java code
23. Everything is an Object
● Supports object oriented programming
● Including primitive types, everything in scala is an object
Example:
● val x = 10
● x.toString()
28. Operators are also a Function
For example
● val sum = 2 + 3
Is same as,
● val sum = 2.+(3) //because ‘+’ operator here is a function
29. Functions in Scala
● Higher order function
● Anonymous function
● Currying
● Tail recursion
30. Defining a Function in Scala
● Defining a function
In Scala -
def sum(x:Int,y:Int) : Int = x+y
In Java -
public int sum(int x,int y){
return x+y;
}
31. Assigning function to variable
Example :
def add = (x:Int,y:Int) => x+y
add(2,3) //should return 5
32. Higher Order Functions
Function that takes functions as a parameters or return function as
a result is called higher order functions
Example : com.ganesh.functions.HigherOrderFunctions.scala
33. Anonymous Function
Scala provides a relatively lightweight syntax for defining
anonymous functions.
Example:
(x: Int) => x + 1
Shorthand for above function is,
new Function1[Int, Int] {
def apply(x: Int): Int = x + 1
}
34. Currying
Currying allows to turn a function that expects two arguments into a
function that expects only one, and that function returns a function
that expects the second argument. Creating basically a chain of
functions.
Example : com.ganesh.functions.Currying.scala
35. Implicit Parameters
The final parameter list on a method can be marked implicit, which
means the values will be taken from the context in which they are
called.
● It means that if no value is supplied when called, the compiler
will look for an implicit value and pass it in for you.
● Implicits are useful in defining default implementations
available. When you need a custom implementation, you can
pass one in explicitly.
Example : com.ganesh.implicits.Implicits.scala
36. Tail Recursion
● Tail recursion is a special kind of recursion where the recursive
call is the very last thing in the function.
● tail-recursive function will only use a single stack frame as
opposed to hundreds or even thousands in case of normal
recursive function.
● Tail recursion is much faster because it executes using single
stack frame
Example : com.ganesh.functions.TailRecursion.scala
37. Transformation on Collections
We can perform different transformations on collection using
collection API.
Example:
com.ganesh.transformations.SimpleTransformations.scala
com.ganesh.transformations.AdvancedTransformations.scala
38. Error Handling
Scala does not have checked exceptions like Java, so you can't do
something like this to force a programmer to deal with an
exception.
Example :
com.ganesh.exceptionhandling.ExceptionThrowing.scala
com.ganesh.exceptionhandling.UsingOptions.scala
39. References
● https://www.coursera.org/learn/progfun1/ - Scala course by
Martin Odersky
● http://www.dipmat.unict.it/~barba/PROG-LANG/PROGRAMMI-
TESTI/READING-MATERIAL/ShortIntroFPprog-lang.htm -
Functional programming and lambda calculus
● Functional Programming in Scala