Thanks to visit codestin.com
Credit goes to www.slideshare.net

Teach Yourself some
Functional Programming
with Scala
@DamianJureczko
Agenda
What is Functional Programming?
Key concepts of Functional Programming
Functional Programming?
Programming Paradigm
Style
There are others
Imperative
Object-oriented
...
Functional Programming is about
pure functions
no side effects
Imperative vs. Functional
Imperative example ✗
List<User> users = new ArrayList<>();
users.add(new User("John", 35));
users.add(new User("Michael", 27));
int count = 0;
for (User user : users)
{
if (user.getAge() > 30)
{
count += 1;
}
}
Functional example ✓
val users = List(User("John", 35), User("Michael", 27))
val count = users.count(user => user.age > 30)
Referential transparency
“An expression is said to be referentially
transparent if it can be replaced with its
corresponding value without changing
the program's behavior.“
Referentially transparent function
returns the same value for same arguments
has no side effects
The same result for same arguments ✓
def inc(x: Int): Int = x + 1
val two = inc(1)
Result can be different for same arguments ✗
def crazyInc(x: Int): Int = x + Random.nextInt()
val whoKnows = crazyInc(1)
Result depends on some global, mutable state ✗
var userCount = ???
def getUserCount: Int = userCount
Function without side effects
Result: x = 8
def add(x: Int, y: Int): Int = x + y
val x = add(1, 1) * add(2, 2)
We can change it to ✓
Result: x = 8
val x = add(1, 1) * 4
We can change it to ✓
Result: x = 8
val x = 2 * 4
Function with side effects
Result: x = 8, totalSum = 6
var totalSum = 0
def weirdAdd(x: Int, y: Int): Int = {
val sum = x + y
totalSum += sum
sum
}
val x = weirdAdd(1, 1) * weirdAdd(2, 2)
We cannot change it to ✗
Result: x = 8, totalSum = 2
val x = weirdAdd(1, 1) * 4
We cannot change it to ✗
Result: x = 8, totalSum = 0
val x = 2 * 4
Pure functions are referentially
transparent
Functional Programming
What are the benefits?
compositionality
(divide & conquer, generalize, reuse)
code easier to reason about
code easier to parallelize
code easier to optimize (memoization)
code easier to test
First-class functions
We can use functions like we use other types
assign to variable
pass to function (as argument)
return from function
Function type
Int => Int
type Transformation = Int => Int
Function object
Anonymous function
x => x + 1
Assigning function to variable
val inc: Int => Int = x => x + 1
val inc: Transformation = x => x + 1
val two = inc(1)
High-order functions
take other functions as argument
return functions as result
Passing function to high-order function
val count = users.count(user => user.age > 30)
def count(p: User ⇒ Boolean): Int
Returning function from high-order function
def makeUserFilterByAge(threshold: Int): User => Boolean =
user => user.age > threshold
val userOlderThen30: User => Boolean = makeUserFilterByAge(30)
val count = users.count(userOlderThen30)
Summary
Functional programming is here
It simplifies your life
It makes you a better developer
Scala is a good way to start
Thank You
@DamianJureczko

Teach Yourself some Functional Programming with Scala