gul uc3m - introduction to functional programming

32
@voiser Introduction to FP

Upload: david-munoz-diaz

Post on 15-Jul-2015

653 views

Category:

Software


6 download

TRANSCRIPT

Page 1: GUL UC3M - Introduction to functional programming

@voiser

Introduction to FP

Page 2: GUL UC3M - Introduction to functional programming

“Functional programming is a style of programming which models computations as the evaluation of expressions.”

https://www.haskell.org/haskellwiki/Functional_programming

Page 3: GUL UC3M - Introduction to functional programming

imperative style vs functional style

Page 4: GUL UC3M - Introduction to functional programming

recipes vs expressions

Page 5: GUL UC3M - Introduction to functional programming

http://commons.wikimedia.org/wiki/File:Turnstile_state_machine_colored.svg

Page 6: GUL UC3M - Introduction to functional programming
Page 7: GUL UC3M - Introduction to functional programming

def sort(array):

less = []

equal = []

greater = []

if len(array) > 1:

pivot = array[0]

for x in array:

if x < pivot:

less.append(x)

if x == pivot:

equal.append(x)

if x > pivot:

greater.append(x)

return sort(less)+equal+sort(greater)

else:

return array

http://stackoverflow.com/questions/18262306/quick-sort-with-python

Page 8: GUL UC3M - Introduction to functional programming

def qsort1(list):

if list == []:

return []

else:

pivot = list[0]

lesser = qsort1([x for x in list[1:] if x < pivot])

greater = qsort1([x for x in list[1:] if x >= pivot])

return lesser + [pivot] + greater

http://en.literateprograms.org/Quicksort_(Python)

Page 9: GUL UC3M - Introduction to functional programming

“Many programming languages support programming in both functional and imperative style but the syntax and facilities of a language are typically optimised for only one of these styles, and social factors like coding conventions and libraries often force the programmer towards one of the styles.”

https://www.haskell.org/haskellwiki/Functional_programming

Page 10: GUL UC3M - Introduction to functional programming

qsort [] = []

qsort (p:xs) = qsort1 lesser ++ [p] ++ qsort1 greater

where

lesser = [ y | y <- xs, y < p ]

greater = [ y | y <- xs, y >= p ]

http://en.literateprograms.org/Quicksort_(Haskell)

Page 11: GUL UC3M - Introduction to functional programming

first class functions*

* Also available in your favourite imperative language

Page 12: GUL UC3M - Introduction to functional programming

interface Filter<T> {

boolean filters(T t);

}

List<T> filter(List<T> original, Filter<T> filter) {

List<T> newList = new LinkedList<T>();

for (T elem : original) {

if (filter.filters(elem)) {

newList.add(elem);

}

}

return newList;

}

Page 13: GUL UC3M - Introduction to functional programming

List<Integer> myList = …

filter(myList, new Filter<Integer>() {

boolean filters(Integer t) {

return t > 10;

}

});

Page 14: GUL UC3M - Introduction to functional programming

filter f xs = [x | x <- xs, f x]

Page 15: GUL UC3M - Introduction to functional programming

filter (\x -> x > 10) myList

Page 16: GUL UC3M - Introduction to functional programming

dealing with state, the functional way*

Page 17: GUL UC3M - Introduction to functional programming

Response dispatch(Request req) {

if (config.blah()) {

if (config.bleh()) {

return dispatchCase1(req)

} else {

return dispatchCase2(req)

}

} else {

if (config.bleh()) {

return dispatchCase3(req)

} else {

return dispatchCase4(req)

}

}

}

Page 18: GUL UC3M - Introduction to functional programming

Response dispatch(Request req) {

int case = config.getDispatchCase()

switch (case) {

case 1: return dispatchCase1(req)

case 2: return dispatchCase2(req)

case 3: return dispatchCase3(req)

case 4: return dispatchCase4(req)

}

}

// when more cases arrive, this code must be updated -> %&#$!!!

Page 19: GUL UC3M - Introduction to functional programming

interface Dispatcher {

Response dispatch(Request);

}

Map[Configuration, Dispatcher] dispatchers = …

Response dispatch (Request req) {

dispatchers.get(config).dispatch(req)

}

Page 20: GUL UC3M - Introduction to functional programming

Type Dispatcher = (Request => Response)

val dispatchers : Map[Configuration, Dispatcher] = ...

def dispatch (req : Request) = dispatchers.get(config)(req)

Page 21: GUL UC3M - Introduction to functional programming

val dispatchers : Stack[Dispatcher] = ...

def dispatch(req : Request) = dispatchers.top()(req)

// some event arrives, starting a transitory state

val d : Dispatcher = { r : Request => ... }

dispatchers.push(d)

// the transitory state finishes, fall back to latest state

dispatchers.pull()

Page 22: GUL UC3M - Introduction to functional programming

lazy evaluation*

Page 23: GUL UC3M - Introduction to functional programming

def fibFrom(a: Int, b: Int): Stream[Int] = a #:: fibFrom(b, a + b)

val fibs = fibFrom(1, 1)

Page 24: GUL UC3M - Introduction to functional programming

immutability*

* Language independent, but surprisingly, some languages encourage mutability.

Page 25: GUL UC3M - Introduction to functional programming

class Person {

string name

int age;

boolean married;

// getters and setters

}

function getInfo(Person p) {

return p.getName()

+ “ is “

+ p.getAge()

+ “ years old and is “

+ (p.isMarried()? “married” : “not married”)

}

function update(Person p, int age, married m) {

p.setAge(age);

p.setMarried(m);

}

Page 26: GUL UC3M - Introduction to functional programming

class Person {

final string name;

final int age;

final boolean married;

}

function getInfo(Person p) {

return p.name

+ “ is ”

+ p.age

+ “ years old and is “

+ (p.married? “married” : “not married”)

}

function update(Person p, int age, married m) {

return new Person(p.name, age, married)

}

Page 27: GUL UC3M - Introduction to functional programming

avoid mutability with (tail) recursion*

Page 28: GUL UC3M - Introduction to functional programming

square [] = []

square (x:xs) = x * x : square xs

Page 29: GUL UC3M - Introduction to functional programming

Q: Is FP perfect for everything?A: No.

Page 30: GUL UC3M - Introduction to functional programming

Q: Should I learn a new language?A: No. You can start adopting functional patterns in your current favourite language.

Page 31: GUL UC3M - Introduction to functional programming

Q: What if I want to learn a new language?A: Well, my personal recommendation is...

Page 32: GUL UC3M - Introduction to functional programming

gul.es@guluc3m