Teach Yourself some Functional Programming with Scala

30
Teach Yourself some Functional Programming with Scala @DamianJureczko

Transcript of Teach Yourself some Functional Programming with Scala

Teach Yourself someFunctional Programming

with Scala

@DamianJureczko

AgendaWhat is Functional Programming?Key concepts of Functional Programming

Functional Programming?Programming Paradigm

Style

There are othersImperativeObject-oriented...

Functional Programming is aboutpure 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 referentiallytransparent if it can be replaced with itscorresponding value without changing

the program's behavior.“

Referentially transparent functionreturns the same value for same argumentshas 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 = 8val x = add(1, 1) * 4

We can change it to ✓

Result: x = 8val 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 = 2val x = weirdAdd(1, 1) * 4

We cannot change it to ✗

Result: x = 8, totalSum = 0val x = 2 * 4

Pure functions are referentiallytransparent

Functional ProgrammingWhat are the benefits?

compositionality (divide & conquer, generalize, reuse)code easier to reason aboutcode easier to parallelizecode easier to optimize (memoization)code easier to test

First-class functionsWe can use functions like we use other types

assign to variablepass to function (as argument)return from function

Function typeInt => Int

type Transformation = Int => Int

Function objectAnonymous function

x => x + 1

Assigning function to variableval inc: Int => Int = x => x + 1

val inc: Transformation = x => x + 1

val two = inc(1)

High-order functionstake other functions as argumentreturn functions as result

Passing function to high-order functionval count = users.count(user => user.age > 30)

def count(p: User ⇒ Boolean): Int

Returning function from high-order functiondef makeUserFilterByAge(threshold: Int): User => Boolean = user => user.age > threshold

val userOlderThen30: User => Boolean = makeUserFilterByAge(30)

val count = users.count(userOlderThen30)

SummaryFunctional programming is hereIt simplifies your lifeIt makes you a better developerScala is a good way to start

Thank You

@DamianJureczko