an introduction to functional programming - developerug - 20140311

80
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now An Introduction to Functional Programming Andreas Pauley – @apauley Lambda Luminaries DeveloperUG March 11, 2014

Upload: andreas-pauley

Post on 11-May-2015

609 views

Category:

Technology


1 download

DESCRIPTION

Functional Programming has received increased attention in recent years. Some people claim that it provides important benefits to programming, but it seems somewhat inaccessible. You have to navigate through lots of academic-speak and look at examples that might only make sense to a professor in mathematics. In this presentation I try to present some of the essential ideas behind functional programming, with simple examples first in Python and then in Haskell. What do you need to know in order to enjoy this talk? I have made some of the following assumptions about the kind of developer who will benefit from this talk: 1. You are a programmer using any programming language 2. You can read Python examples (it's WAY shorter on slides than C# or Java) 3. You are interested enough in improving your code that you are willing to challenge some common assumptions.

TRANSCRIPT

Page 1: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

An Introduction to Functional Programming

Andreas Pauley – @apauleyLambda Luminaries

DeveloperUG

March 11, 2014

Page 2: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

@lambdaluminary

We meet once a month, on the second Monday of the month.

http://www.meetup.com/lambda-luminaries/

Page 3: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Jemstep

Retirement portfolio analysis in Scala.

http://www.jemstep.com/

Page 4: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Page 5: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Page 6: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

“ No matter what language you work in,programming in a functional style provides benefits.You should do it whenever it is convenient, and youshould think hard about the decision when it isn’tconvenient. ”

— John Carmack, ID Software [2]

Page 7: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Quake

Page 8: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

But what exactly is“Functional Programming”?

Page 9: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Functional Programming, noun:

Functional Programming is alist of things you CAN’T do.

[7]

Page 10: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

You can’t vary your variables

1> X = 42.

42

2> X = X + 1.

** exception error:

no match of right hand

side value 43

Page 11: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

No while/for loops. Sorry :-(

int i;

for (i=1; i<=3; i++) {

System.out.println(i);

}

Page 12: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

You can’t mutate/change your data structures

Python

>>> list1 = [1,2,3]

>>> list2 = list1

>>> print list1.reverse()

None

>>> list1

[3, 2, 1]

>>> list2

[3, 2, 1]

Haskell

> let list1 = [1,2,3]

> let list2 = list1

> reverse(list1)

[3,2,1]

> list1

[1,2,3]

> list2

[1,2,3]

Page 13: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

You can’t have any side effects

Page 14: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Are you kidding me?

How can anyone program like this???

Page 15: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

GOTO 10

This sounds like

“You can’t have GOTO statements”

See Hughes and Dijkstra [1, 3]

Page 16: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

It’s not about what we cannotdo.

Page 17: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

We need a better definition ofFunctional Programming.

Page 18: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Programming Paradigms(Very Simplified)

Imperative Declarative

Page 19: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Functional Programming, noun:

“ Functional programming is so called because aprogram consists entirely of functions. ”

— John Hughes, Why Functional Programming Matters [1, p. 1]

Page 20: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

OK... so what exactly is a function?

Page 21: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

An example function

f (x) = 2x2 − 2x + 3

−1 1 2 3

4

6

8

10

12

14

x

y

Page 22: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Variables in functions

f (x) = 2x2 − 2x + 3When we evaluate the function:f(2) = 8− 4 + 3 = 7

• The value of x will not change inside the function body.

• Same input, same output. Every time. (ReferentialTransparency)

• We can call f multiple times without any side effects(Idempotence).

• We don’t have to recalculate f(2), we can replace anyoccurrence of f(2) with 7 (Memoization).

Page 23: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Variables in functions

f (x) = 2x2 − 2x + 3When we evaluate the function:f(2) = 8− 4 + 3 = 7

• The value of x will not change inside the function body.

• Same input, same output. Every time. (ReferentialTransparency)

• We can call f multiple times without any side effects(Idempotence).

• We don’t have to recalculate f(2), we can replace anyoccurrence of f(2) with 7 (Memoization).

Page 24: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Variables in functions

f (x) = 2x2 − 2x + 3When we evaluate the function:f(2) = 8− 4 + 3 = 7

• The value of x will not change inside the function body.

• Same input, same output. Every time. (ReferentialTransparency)

• We can call f multiple times without any side effects(Idempotence).

• We don’t have to recalculate f(2), we can replace anyoccurrence of f(2) with 7 (Memoization).

Page 25: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Variables in functions

f (x) = 2x2 − 2x + 3When we evaluate the function:f(2) = 8− 4 + 3 = 7

• The value of x will not change inside the function body.

• Same input, same output. Every time. (ReferentialTransparency)

• We can call f multiple times without any side effects(Idempotence).

• We don’t have to recalculate f(2), we can replace anyoccurrence of f(2) with 7 (Memoization).

Page 26: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Varying variables does not make sense

x = x + 1

x− x = 1

0 = 1

∴ x 6= x + 1

Page 27: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Functions can call other functions

g(x) = f (x) + 1

Page 28: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Values are functions

Constant values are just functions with no input parameters

x = 42

Python function definition:

def x():

return 42

Haskell function definition:

x = 42

Page 29: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Functions can be composed

h(x) = (f ◦ g)(x) = f (g(x))

or

h = f ◦ g

Page 30: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Higher-order Functions

Functions can take functions as input.Functions can return functions as the result.

h(f, g, x) = f (x) + g(2)

Page 31: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Higher-order FunctionsThe derivative of f(x) returns another function.

f(x) = 2x2 − 2x+ 3ddxf(x) = 4x− 2

−1 1 2 3

−5

5

10

15

x

y

Page 32: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

A functional program consists entirely of functions

def main():

time = datetime.now()

args = sys.argv[1:]

print outputString(time, args)

def outputString(time, args):

return str(time) + " " + joinArgs(args)

def joinArgs(args):

return "-".join(args)

$ ./justfunctions.py Hello from Python

2014-02-15 10:36:42.062697 Hello-from-Python

Page 33: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

A functional program consists entirely of functions

main :: IO()

main = do

time <- getCurrentTime

args <- getArgs

putStrLn (outputString time args)

outputString :: UTCTime -> [String] -> String

outputString time args =

show(time) ++ " " ++ joinArgs(args)

joinArgs :: [String] -> String

joinArgs = intercalate "-"

$ ./justfunctions Hello from Haskell

2014-02-15 08:36:50.822728 UTC Hello-from-Haskell

Page 34: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Some Haskell Syntax

Python:

def outputString(time, args):

return str(time) + " " + joinArgs(args)

Haskell:

outputString :: UTCTime -> [String] -> String

outputString time args =

show(time) ++ " " ++ joinArgs(args)

Page 35: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Recursive function: Haskell

doubleAll :: [Int] -> [Int]

doubleAll [] = []

doubleAll (x:xs) = x*2 : doubleAll xs

Example use in the interactive interpreter:

Prelude Main> doubleAll [8,2,3]

[16,4,6]

Page 36: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Recursive function expanded

doubleAll [] = []

doubleAll (x:xs) = x*2 : doubleAll xs

doubleAll [8,2,3]

16 : (doubleAll [2,3])

16 : 4 : (doubleAll [3])

16 : 4 : 6 : (doubleAll [])

16 : 4 : 6 : []

16 : 4 : [6]

16 : [4,6]

[16,4,6]

Page 37: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Recursive function: Python

def doubleAll(numbers):

if numbers == []:

return []

else:

first = numbers[0]

rest = numbers[1:]

return [first * 2] + doubleAll(rest)

Example use in the interactive interpreter:

>>> doubleAll([8,2,3])

[16, 4, 6]

Page 38: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Pattern Matching: Haskell

doubleAll :: [Int] -> [Int]

doubleAll [] = []

doubleAll (x:xs) = x*2 : doubleAll xs

Page 39: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Higher-order Functions3 Basic List Operations

Map Convert each element of a list into some other value.

Filter Get a subset of a list based on some condition.

Fold Reduce a list of items to a single value.

Page 40: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Higher-order Functions3 Basic List Operations

Map Convert each element of a list into some other value.

Filter Get a subset of a list based on some condition.

Fold Reduce a list of items to a single value.

Page 41: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Higher-order Functions3 Basic List Operations

Map Convert each element of a list into some other value.

Filter Get a subset of a list based on some condition.

Fold Reduce a list of items to a single value.

Page 42: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Map

Apply a function to each element of a list, and you get a new list.

a1 a2 a3 ... an

b1 b2 b3 ... bn

f(a1) f(an)

Page 43: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Map

The built-in Haskell map function:

map :: (a -> b) -> [a] -> [b]

map _ [] = []

map f (x:xs) = f x : map f xs

Page 44: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Map

The builtin Haskell map function:

map :: (a -> b) -> [a] -> [b]

map _ [] = []

map f (x:xs) = f x : map f xs

Hmmm, looks very similar to our previous doubleAll function:

doubleAll :: [Int] -> [Int]

doubleAll [] = []

doubleAll (x:xs) = x*2 : doubleAll xs

Page 45: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Map

So our doubleAll can actually be simplified.

In Haskell:

doubleAll = map (*2)

In Python:

def doubleAll(numbers):

return map(lambda x: x * 2, numbers)

Page 46: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Map

doubleAll :: Num a => [a] -> [a]

doubleAll = map (*2)

8 2 3

16 4 6

∗2 ∗2 ∗2

Page 47: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Account Data

data Bank = ABSA | Capitec | FNB | Nedbank | SBSA

data Account = Account {bank :: Bank,

accNum :: String,

owner :: String,

balance :: Amount}

Page 48: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Account Data

[Account {accNum="4076814233",

owner="J. Doe",

balance=(Amount 123000.23),

bank=ABSA},

Account {accNum="6868773585",

owner="J. Black",

balance=(Amount 5782347.99),

bank=FNB},

Account {accNum="4055892156",

owner="A. Kay",

balance=(Amount 100),

bank=ABSA},

Account {accNum="6584539813",

owner="S. Jones",

balance=(Amount 2937361.45),

bank=FNB}]

Page 49: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Map

Map on account data:

balances :: [Account] -> [Amount]

balances accounts = map balance accounts

acc1 acc2 acc3 acc4 acc5

bal1 bal2 bal3 bal4 bal5

balance(acc1) balance(acc5)

Page 50: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Filter

acc1 acc2 acc3 acc4 acc5

acc1 acc3 acc5

in? in? in? in? in?

Page 51: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Filter

Filter on account data:

topAccounts :: [Account] -> [Account]

topAccounts accounts = filter isRich accounts

isRich :: Account -> Bool

isRich acc = balance acc >= (Amount 1000000)

Output:

*Main> topAccounts accounts

[FNB 6868773585 (J. Black) R5782347.99,

FNB 6584539813 (S. Jones) R2937361.45]

Page 52: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Fold/Reduce/Inject

foldl (+) 0 [8,2,3]

13

0 8 2 3

8 2 3

10 3

13

Page 53: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Fold/Reduce/Inject

balanceSum :: [Account] -> Amount

balanceSum accounts = foldl (+) 0 (balances accounts)

0 bal1 bal2 bal3

sum bal2 bal3

sum bal3

sum

Page 54: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Function Composition

balanceSum :: [Account] -> Amount

balanceSum accounts = foldl (+) 0 (balances accounts)

0 bal1 bal2 bal3

sum bal2 bal3

sum bal3

sum

s2 :: [Account] -> Amount

s2 = balances |> (foldl(+)0)

h(x) = (f ◦ g)(x) = f(g(x))

h = f ◦ g

s3 = f1 ◦ balances

s3 :: [Account] -> Amount

s3 = (foldl(+)0) . balances

Page 55: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

balancesPerBank

foldl insertBalance Map.empty accounts

{} acc1 acc2 acc3

map acc2 acc3

map acc3

map

Page 56: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Fold

type BankMap = Map Bank Amount

balancesPerBank :: [Account] -> BankMap

balancesPerBank = foldl insertBalance Map.empty

Output:

*Main> balancesPerBank accounts

fromList [(ABSA,R123100.23),(FNB,R8719709.44)]

Page 57: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Fold

type BankMap = Map Bank Amount

balancesPerBank :: [Account] -> BankMap

balancesPerBank = foldl insertBalance Map.empty

insertBalance :: BankMap -> Account -> BankMap

insertBalance bankmap account =

Map.insert key value bankmap

where key = bank account

value = addBalance bankmap account

addBalance :: BankMap -> Account -> Amount

addBalance bankmap account =

case (Map.lookup (bank account) bankmap) of

Nothing -> balance account

Just bal -> (balance account) + bal

Page 58: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Programming Paradigms(Very Simplified)

Imperative Declarative

Page 59: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

In imperative programming:

• Your variables can vary any time!

• You have to use locks to be thread-safe!

• You have to write your own loops for the most basic listoperations!

• Your data structures are mutable!

• You have to defensively make copies of data to prevent bugs!

• You have to defensively check for null values!

• You have to think about implicit state! (this, self)

• Code is generally riddled with side effects!

Page 60: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

In imperative programming:

• Your variables can vary any time!

• You have to use locks to be thread-safe!

• You have to write your own loops for the most basic listoperations!

• Your data structures are mutable!

• You have to defensively make copies of data to prevent bugs!

• You have to defensively check for null values!

• You have to think about implicit state! (this, self)

• Code is generally riddled with side effects!

Page 61: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

In imperative programming:

• Your variables can vary any time!

• You have to use locks to be thread-safe!

• You have to write your own loops for the most basic listoperations!

• Your data structures are mutable!

• You have to defensively make copies of data to prevent bugs!

• You have to defensively check for null values!

• You have to think about implicit state! (this, self)

• Code is generally riddled with side effects!

Page 62: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

In imperative programming:

• Your variables can vary any time!

• You have to use locks to be thread-safe!

• You have to write your own loops for the most basic listoperations!

• Your data structures are mutable!

• You have to defensively make copies of data to prevent bugs!

• You have to defensively check for null values!

• You have to think about implicit state! (this, self)

• Code is generally riddled with side effects!

Page 63: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

In imperative programming:

• Your variables can vary any time!

• You have to use locks to be thread-safe!

• You have to write your own loops for the most basic listoperations!

• Your data structures are mutable!

• You have to defensively make copies of data to prevent bugs!

• You have to defensively check for null values!

• You have to think about implicit state! (this, self)

• Code is generally riddled with side effects!

Page 64: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

In imperative programming:

• Your variables can vary any time!

• You have to use locks to be thread-safe!

• You have to write your own loops for the most basic listoperations!

• Your data structures are mutable!

• You have to defensively make copies of data to prevent bugs!

• You have to defensively check for null values!

• You have to think about implicit state! (this, self)

• Code is generally riddled with side effects!

Page 65: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

In imperative programming:

• Your variables can vary any time!

• You have to use locks to be thread-safe!

• You have to write your own loops for the most basic listoperations!

• Your data structures are mutable!

• You have to defensively make copies of data to prevent bugs!

• You have to defensively check for null values!

• You have to think about implicit state! (this, self)

• Code is generally riddled with side effects!

Page 66: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

In imperative programming:

• Your variables can vary any time!

• You have to use locks to be thread-safe!

• You have to write your own loops for the most basic listoperations!

• Your data structures are mutable!

• You have to defensively make copies of data to prevent bugs!

• You have to defensively check for null values!

• You have to think about implicit state! (this, self)

• Code is generally riddled with side effects!

Page 67: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Are you kidding me?

How can anyone program like this???

Page 68: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Page 69: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Join the anti-for campaign

Less loops, moremap/filter/fold

http://weblogs.asp.net/podwysocki/archive/2009/06/26/

the-anti-for-campaign.aspx

Page 70: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Treat side effects as a first-class concern

Page 71: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Learn a functional language

“ A language that doesn’t affect the way you thinkabout programming, is not worth knowing. ”

— Alan Perlis[5]

Page 72: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Disclaim your inheritance

Write non-trivial code withoutusing objects and inheritance.Get re-usability with higher-order functions.

Try to minimise moving parts instead ofencapsulating moving parts. [4]

Page 73: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Join our group@lambdaluminary

We meet once a month, on the second Monday of the month.http://www.meetup.com/lambda-luminaries/

Page 74: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Get out of your comfort zone

Functional Programming is unfamiliar territory formost.

“ If you want everything to be familiar you willnever learn anything new. ”

— Rich Hickey, author of Clojure[6]

Page 75: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Companies In South Africa

Jemstep, Sandton Using Scala for Fund Analysis

Allan Gray, Cape Town Using Scala for backend logic and systemintegration.

Yuppiechef, Cape Town Using Clojure for their WarehouseManagement System.

Cognician, Cape Town Using Clojure to create coaching/learningmodules.

Eldo Energy, Johannesburg Using Clojure for automated meterreading and intelligent monitoring of consumerenergy.

Rheo Systems, Pretoria Using Clojure for supply chain integration.

Page 76: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Companies In South Africa

Pattern Matched Technologies, Midrand Using Erlang for allsystems, eg. processing high volumes of financialtransactions.

Effective Control Systems, Kyalami Using Erlang for printermanagement.

Mira Networks, Somerset West Using Erlang for billingadministration and mobile development.

Kotive Using Scala for designing workflow processes.

Page 77: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Online Courses

Functional Thinking by Neal FordO’ Reillyhttp://shop.oreilly.com/product/0636920030393.do

Functional Programming Principles in ScalaEPFL Universityhttps://www.coursera.org/course/progfun

School of HaskellFP Completehttps://www.fpcomplete.com/school

Programming LanguagesUniversity of Washingtonhttps://www.coursera.org/course/proglang

Page 78: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

Books

Miran LipovacaLearn You a Haskell for Great Good!http://learnyouahaskell.com/

Fred HebertLearn You Some Erlang for Great Good!http://learnyousomeerlang.com/

Yaron Minski, Anil Madhavapeddy, Jason HickeyReal World OCamlhttps://realworldocaml.org/

Paul Chiusano, Runar BjarnasonFunctional Programming in Scalahttp://www.manning.com/bjarnason/

Page 79: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

References I

John HughesWhy Functional Programming Mattershttp://www.cs.kent.ac.uk/people/staff/dat/miranda/

whyfp90.pdf

John CarmackFunctional Programming in C++http://www.altdevblogaday.com/2012/04/26/

functional-programming-in-c/

Edsger W. DijkstraGo To Statement Considered Harmfulhttp://www.u.arizona.edu/~rubinson/copyright_

violations/Go_To_Considered_Harmful.html

Page 80: An Introduction to Functional Programming - DeveloperUG - 20140311

Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now what?

References II

Tweet by Michael Feathershttps://twitter.com/mfeathers/status/29581296216

Alan Jay Perlishttp://www.cs.yale.edu/quotes.html

Rich Hickeyhttp:

//www.infoq.com/presentations/Simple-Made-Easy

Andreas PauleyAn Introduction to Functional Programminghttps://github.com/apauley/fp_presentation