Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Functional Programming is the new black

elise_huard
October 07, 2012
1.2k

Functional Programming is the new black

elise_huard

October 07, 2012
Tweet

Transcript

  1. It’s all functions no global variables or global state no

    side-effects no destructive updates Sunday, 7 October 12
  2. 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
  3. 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
  4. 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
  5. var phoneListing = function(name,home) { return name + “ -

    “ + phone; } listing(“Sam”,”012345678”); listing(“Sam”); // => undefined Sunday, 7 October 12
  6. // 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
  7. // make a monadic value (“return”) var makeMonadicValue = function(value)

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

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

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

    mv(10); // => {value: "Joe", state: 10} Sunday, 7 October 12
  11. // 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
  12. // 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
  13. // 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
  14. (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
  15. “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