Slide 1

Slide 1 text

Sunday, 7 October 12

Slide 2

Slide 2 text

Plan B Sunday, 7 October 12

Slide 3

Slide 3 text

Why functional is the new black Elise Huard @elise_huard JSConf EU 2012 Sunday, 7 October 12

Slide 4

Slide 4 text

Why functional is the new black Elise Huard @elise_huard JSConf EU 2012 Sunday, 7 October 12

Slide 5

Slide 5 text

Why functional is the new black Elise Huard @elise_huard JSConf EU 2012 Sunday, 7 October 12

Slide 6

Slide 6 text

Sunday, 7 October 12

Slide 7

Slide 7 text

λ λ Sunday, 7 October 12

Slide 8

Slide 8 text

λ λ Sunday, 7 October 12

Slide 9

Slide 9 text

Sunday, 7 October 12

Slide 10

Slide 10 text

Sunday, 7 October 12

Slide 11

Slide 11 text

It’s all functions Sunday, 7 October 12

Slide 12

Slide 12 text

It’s all functions no global variables or global state Sunday, 7 October 12

Slide 13

Slide 13 text

It’s all functions no global variables or global state no side-effects Sunday, 7 October 12

Slide 14

Slide 14 text

It’s all functions no global variables or global state no side-effects no destructive updates Sunday, 7 October 12

Slide 15

Slide 15 text

function Person(gender) { this.gender = gender; } Person.prototype.sayGender = function() { console.log(this.gender); }; var person1 = new Person('Male'); var genderTeller = person1.sayGender; Sunday, 7 October 12

Slide 16

Slide 16 text

Sunday, 7 October 12

Slide 17

Slide 17 text

Some “patterns” Sunday, 7 October 12

Slide 18

Slide 18 text

Higher order functions Sunday, 7 October 12

Slide 19

Slide 19 text

Taking functions as arguments Sunday, 7 October 12

Slide 20

Slide 20 text

input.change(function(event) {...}); app.get('/', function(req, res) { ...}); Sunday, 7 October 12

Slide 21

Slide 21 text

returning functions Sunday, 7 October 12

Slide 22

Slide 22 text

var validate = function(validationFunction) { return function(field) { var isError, errorMessage = validationFunction(field); return (isError,errorMessage); } } var requiredField = validate(function(field) { var errorMessage = null; if (!field) { errorMessage = "should have a field" } return [!field, errorMessage]; }) Sunday, 7 October 12

Slide 23

Slide 23 text

Utilities Sunday, 7 October 12

Slide 24

Slide 24 text

[1,3,5].map(function(x) {return x+1 }); http://underscorejs.org/ _.map([1,2,3], function(x) {return x+1}); http://osteele.com/sources/javascript/functional/ map('x+1', [1,2,3,4]) Sunday, 7 October 12

Slide 25

Slide 25 text

for (var i = 0; i < array.length; i++) doSomething(array[i]); // => map for (var i = 0; i < array.length; i++) { if (array[i]%2 == 0) doSomething(array[i]); } // => filter var sum = 0; for (var i = 0; i < array.length; i++) { sum += array[i]; } // => reduce/foldl http://notes-on-haskell.blogspot.co.uk/2007/02/whats-wrong-with-for-loop.html Sunday, 7 October 12

Slide 26

Slide 26 text

Currying Sunday, 7 October 12

Slide 27

Slide 27 text

var phoneListing = function(name,home) { return name + “ - “ + phone; } listing(“Sam”,”012345678”); listing(“Sam”); // => undefined Sunday, 7 October 12

Slide 28

Slide 28 text

// getLocalArgs() = Array.prototype.slice.call(arguments) function partial(f) { var args = getLocalArgs(), givenArgs = args.slice(1); return function() { var remainingArgs = getLocaLArgs(); return f.apply(this, givenArgs.concat(remainingArgs)); } } var samsPartner= partial(phoneListing, ”Sam”); samsPartner(“0999999999”); // => “Sam - 0999999999” Sunday, 7 October 12

Slide 29

Slide 29 text

Monads Sunday, 7 October 12

Slide 30

Slide 30 text

Sunday, 7 October 12

Slide 31

Slide 31 text

// make a monadic value (“return”) var makeMonadicValue = function(value) { return function(state) { return {value: value, state: state}; } } Sunday, 7 October 12

Slide 32

Slide 32 text

// make a very simple monadic value var makeMonadicValue = function(value) { return function() { return value; } } Sunday, 7 October 12

Slide 33

Slide 33 text

// make a monadic value (“return”) var makeMonadicValue = function(value) { return function(state) { return {value: value, state: state}; } } Sunday, 7 October 12

Slide 34

Slide 34 text

// Joe is in the kitchen var mv = makeMonadicValue("Joe"); mv(10); // => {value: "Joe", state: 10} Sunday, 7 October 12

Slide 35

Slide 35 text

// bind to a function var bindToFunction = function(mv, f) { return function(state) { var monadOutput = mv(state), value = monadOutput.value, newState = monadOutput.state; return (f(value))(newState); } } Sunday, 7 October 12

Slide 36

Slide 36 text

// you could act on the value directly var addPerson = function(value, name) { return value + " and " + name; } // but here's the monadic way to do it var mAddPerson = function(mv, name) { return bindToFunction(mv, function(value) { return makeMonadicValue(value + " and " + name); }) } Sunday, 7 October 12

Slide 37

Slide 37 text

(mAddPerson(mv, "Tom"))(10); // => {value:"Joe and Tom", state: 10} Sunday, 7 October 12

Slide 38

Slide 38 text

// you can act on state var takeYoghurt = function(state) { return state - 1; } var mTakeYoghurt = function(mv) { return bindToFunction(mv, function(value) { return function(state) { return {value: value, state: state-1}; } }) } (mTakeYoghurt(mv))(10); // => {value: "Joe", state: 9 } Sunday, 7 October 12

Slide 39

Slide 39 text

(mTakeYoghurt( (mAddPerson( (mTakeYoghurt( (mAddPerson (mv,"Tom") ))), "Shelley")))) (10); // => { value: 'Joe and Tom and Shelley', state: 8 } http://www.infoq.com/presentations/Why-is-a-Monad-Like-a- Writing-Desk Sunday, 7 October 12

Slide 40

Slide 40 text

Lazy evaluation (?) Sunday, 7 October 12

Slide 41

Slide 41 text

The Good Sunday, 7 October 12

Slide 42

Slide 42 text

Easier to debug Sunday, 7 October 12

Slide 43

Slide 43 text

Easier to test Sunday, 7 October 12

Slide 44

Slide 44 text

Easier to refactor Sunday, 7 October 12

Slide 45

Slide 45 text

Parallelization (?) Sunday, 7 October 12

Slide 46

Slide 46 text

Hard? Sunday, 7 October 12

Slide 47

Slide 47 text

Sunday, 7 October 12

Slide 48

Slide 48 text

"the greatest single programming language ever designed" - Alan Kay, on Lisp, the 70s Sunday, 7 October 12

Slide 49

Slide 49 text

“Object-oriented programming (...) allows software structure to be based on real-world structures, and gives programmers a powerful way to simplify the design and construction of complex programs. - D. Gelernter, the 80s Sunday, 7 October 12

Slide 50

Slide 50 text

Sunday, 7 October 12

Slide 51

Slide 51 text

Software is hard Sunday, 7 October 12

Slide 52

Slide 52 text

Sunday, 7 October 12

Slide 53

Slide 53 text

Sunday, 7 October 12

Slide 54

Slide 54 text

But: Sunday, 7 October 12

Slide 55

Slide 55 text

Thank you. Elise Huard @elise_huard Sunday, 7 October 12