Slide 1

Slide 1 text

FUNCTIONAL PROGRAMMING THE FUTURE IS OLD

Slide 2

Slide 2 text

hi, I'm @jergason

Slide 3

Slide 3 text

who are you?

Slide 4

Slide 4 text

ASSEMBLY

Slide 5

Slide 5 text

.global main .text main: mov eax, 55 ; load two numbers into registers mov ebx, 45 add eax, ebx ; add them (result stored in eax) push eax push ebx ret

Slide 6

Slide 6 text

✨fancy assembly✨ most programming is

Slide 7

Slide 7 text

let userName = prompt('Name, scallywag?') let upperCaseName = userName.toUpperCase() alert('Yar, ' + upperCaseName + ' is a foul appellative indeed.')

Slide 8

Slide 8 text

Assembly (1949) C (1972) C++ (1983) Java (1995) JavaScript (now-ish)

Slide 9

Slide 9 text

Assembly (1949) C (1972) C++ (1983) Java (1995) JavaScript (1995)

Slide 10

Slide 10 text

how CPUs work is a terrible way to program

Slide 11

Slide 11 text

ᕕ( ᐛ )ᕗ

Slide 12

Slide 12 text

BEFORE COMPUTERS

Slide 13

Slide 13 text

Alonzo Church

Slide 14

Slide 14 text

lol i invented lambda calculus

Slide 15

Slide 15 text

FUNCTIONS not INSTRUCTIONS

Slide 16

Slide 16 text

Functions are MAPPINGS

Slide 17

Slide 17 text

x y f(x) = x^2 3 9

Slide 18

Slide 18 text

LISP (1958) ML (1973) Haskell (1990) λ calculus (1932) ISWIM (1966)

Slide 19

Slide 19 text

Why do you care?

Slide 20

Slide 20 text

stateless functions

Slide 21

Slide 21 text

function add(a, b) { fireZeMissiles() return a + b }

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

function add(a, b) { fireZeMissiles() return a + b } var deathToll = 0 function fireZeMissiles() { if (deathToll > 7000000000) { console.log('the world has ended. everyone is dead') return } deathToll += Math.round(Math.random() * 100000000) console.log('Oh no, ', deathToll, ' dead') }

Slide 24

Slide 24 text

return value only determined by input no side effects no dependencies

Slide 25

Slide 25 text

function augment(obj) { obj.myVal = 'foo' return obj } var obj = {} augment(obj) console.log(obj) // {myVal: 'foo'}

Slide 26

Slide 26 text

SHARED MUTABLE STATE

Slide 27

Slide 27 text

function augment(obj) { return _.assign({}, obj, {myVal: 'foo'}) } var obj = {} var newObj = augment(obj) console.log(obj) // {} console.log(newObj) // {myVal: 'foo'}

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

never write for loops

Slide 30

Slide 30 text

var output = [] for (let i = 0; i < array.length; i++) { let changedData = changeData(array[i]) output[i] = changedData }

Slide 31

Slide 31 text

var output = [] for (let i = 0; i < array.length; i++) { let changedData = changeData(array[i]) output[i] = changedData } UGH NO WHY GAH

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

let output = map(changeData, array)

Slide 35

Slide 35 text

function map(f, arr) { var a = [] for (var i = 0; i < arr.length; i++) { a.push(f(arr[i])) } return a }

Slide 36

Slide 36 text

map - transform an array filter - remove items from array reduce - turn array to one value each - do something with each value

Slide 37

Slide 37 text

FUNCTIONS not INSTRUCTIONS

Slide 38

Slide 38 text

currying

Slide 39

Slide 39 text

function split(delimiter, string) { return string.split(delimiter) } console.log(split('.', 'foo.bar')) // ['foo', 'bar']

Slide 40

Slide 40 text

function split(delimiter, string) { return string.split(delimiter) } function splitNewlines(string) { return split('\n', string) }

Slide 41

Slide 41 text

function split(delimiter, string) { return string.split(delimiter) } function splitNewlines(string) { return split('\n', string) } function splitShrug(string) { return split(`¯\_(ツ)_/¯`, string) }

Slide 42

Slide 42 text

function split(delimiter) { return function(string) { string.split(delimiter) } } let splitNewlines = split('\n') let splitShrug = split(`¯\_(ツ)_/¯`)

Slide 43

Slide 43 text

let split = curry(function(delimiter, string) { return string.split(delimiter) }) let splitNewlines = split('\n') let splitShrug = split(`¯\_(ツ)_/¯`)

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

composition

Slide 46

Slide 46 text

function createUser(username) { return { username, password: 'beans' } } function hashUserPassword(user) { return { username: user.username, hash: user.password.toUpperCase() } } function createHashedUser(username) { return hashUserPassword(createUser(username)) }

Slide 47

Slide 47 text

function compose(f, g) { return function(x) { return f(g(x)) } }

Slide 48

Slide 48 text

function createUser(username) { return { username, password: 'beans' } } function hashUserPassword(user) { return { username: user.username, hash: user.password.toUpperCase() } } let createHashedUser = compose(hashUserPassword, createUser)

Slide 49

Slide 49 text

compose(hashUserPassword, createUser) username user

Slide 50

Slide 50 text

let createUser = compose(hashUserPassword, createUser) let u = createUser('jamitron') console.log(u) // {username: 'jamitron', hash: 'BEANS'}

Slide 51

Slide 51 text

separate side effects and stateless code

Slide 52

Slide 52 text

a completely stateless program is useless

Slide 53

Slide 53 text

side-effecting stateless

Slide 54

Slide 54 text

pokemans

Slide 55

Slide 55 text

react

Slide 56

Slide 56 text

elm

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

work at kualico ask me @jergason

Slide 59

Slide 59 text

Sources • http://www.infoq.com/presentations/Taming-Effect-Simon- Peyton-Jones • https://github.com/MostlyAdequate/mostly-adequate-guide • https://www.destroyallsoftware.com/screencasts/catalog/ functional-core-imperative-shell • http://learnyouahaskell.com/ • http://elm-lang.org/ • http://www.infoq.com/presentations/Value-Values

Slide 60

Slide 60 text

Sources • http://www.infoq.com/presentations/Simple-Made-Easy-QCon-London-2012 • https://en.wikipedia.org/wiki/Functional_programming • https://en.wikipedia.org/wiki/Imperative_programming • https://www.youtube.com/watch?v=oYk8CKH7OhE • http://haskell.cs.yale.edu/wp-content/uploads/2011/01/cs.pdf • https://www.cs.kent.ac.uk/people/staff/dat/tfp12/tfp12.pdf • Coders at Work • https://en.wikipedia.org/wiki/Lambda_calculus

Slide 61

Slide 61 text

Images • 2001 vector art - couldn't find original source, see http://wallbasehq.com/images/big/ 2001_a_space_odyssey_space_suits_vector_art_wallpaper-3587.jpg • http://vignette3.wikia.nocookie.net/2001/images/4/41/A_itmes.jpg/revision/latest? cb=20111116211153 • https://sjmj91.files.wordpress.com/2013/02/2001space043.jpg • https://flic.kr/p/5J1zrp • http://i.imgur.com/F2rWHbb.gif • https://i.ytimg.com/vi/Tj6n2YhFuqg/mqdefault.jpg • https://www.flickr.com/photos/bohman/16062901379/in/photolist-qtqyXv-86kuSi-7Xi4jn-abhEnd- uujbAG-abhAEy-abeGDt-o6zUda-dSLBvP-94CfH9-abhvMA-bUUme1-cobi7q-8YEbSy-n9e9eN-a1HRJH- dMBF1h-5woynn-7ZBp9H-9aJJ5m-8teEAc-8eDSbB-6ZwXL1-7bXjDd-8feUCk-9yHDJx-aZbzfr-duPLHD- eWYsiy-6TTARf-74HG5i-duNd4J-cWWjFG-abeLZX-v7jH5m-v7jHJ7-uXyiKa-6oWZC3-e1UNqc-8phgVx- v7jF85-5XuumM-8ioqe2-vpfrnX-abeEt2-abhvq5-7SVK8Z-abeLSk-4hEHCs-abeGya • http://assets22.pokemon.com/assets/cms2/img/pokedex/full/001.png