Save 37% off PRO during our Black Friday Sale! »

Learning Functional Programming with JavaScript

Learning Functional Programming with JavaScript

Given at JSUnconf 2016 (http://2016.jsunconf.eu/)

654527a5cff1756177ef0b1bb0af7aa3?s=128

Anjana Sofia Vakil

April 24, 2016
Tweet

Transcript

  1. Learning Functional Programming with JS @AnjanaVakil

  2. The Recurse Center https://www.recurse.com/

  3. What is functional programming?

  4. What is functional programming? a programming paradigm

  5. What is functional programming? a coding style

  6. What is functional programming? a mindset

  7. What is functional programming? a sexy, buzz-wordy trend

  8. Why functional JavaScript?

  9. Why functional JavaScript? object-oriented JS gets tricky (prototypes? this?!?)

  10. Why functional JavaScript? safer, easier to debug/maintain

  11. Why functional JavaScript? established community

  12. OK, let’s do it!

  13. OK, let’s do it! ...how?

  14. Do everything with functions input -> output

  15. Not functional: var name = “Anjana”; var greeting = “Hi,

    I’m ”; console.log(greeting + name); => “Hi, I’m Anjana”
  16. Functional: function greet(name) { return “Hi, I’m ” + name;

    } greet(“Anjana”); => “Hi, I’m Anjana”
  17. Avoid side effects use “pure” functions

  18. Not pure: var name = “Anjana”; function greet() { console.log(“Hi,

    I’m ” + name); }
  19. Pure: function greet(name) { return “Hi, I’m ” + name;

    }
  20. Use higher-order functions functions can be inputs/outputs

  21. function makeAdjectifier(adjective) { return function (string) { return adjective +

    “ ” + string; }; } var coolifier = makeAdjectifier(“cool”); coolifier(“conference”); => “cool conference”
  22. Don’t iterate use map, reduce, filter

  23. http://www.datasciencecentral.com/forum/topics/what-is-map-reduce

  24. Avoid mutability use immutable data

  25. Mutation (bad!): var rooms = [“H1”, “H2”, “H3”]; rooms[2] =

    “H4”; rooms; => ["H1", "H2", "H4"]
  26. No mutation (good!): var rooms = [“H1”, “H2”, “H3”]; Var

    newRooms = rooms.map(function (rm) { if (rm == “H3”) { return “H4”; } else { return rm; } }); newRooms; => ["H1", "H2", "H4"] rooms; => ["H1", "H2", "H3"]
  27. Persistent data structures for efficient immutability Mori, Immutable.js

  28. Ready to try it out?

  29. FP libraries for JS • Mori (http://swannodette.github.io/mori/) • Immutable.js (https://facebook.github.

    io/immutable-js/) • Underscore (http://underscorejs.org/) • Lodash (https://lodash.com/) • Ramda (http://ramdajs.com/) • ...and more!
  30. Want to learn more?

  31. “An introduction to functional programming” by Mary Rose Cook https://codewords.recurse.com/issues/one/an-introduction-to-functional-programming

  32. Thanks for listening! I’m @AnjanaVakil Huge thanks to: JSUnconf organizers

    Diversity sponsors The Recurse Center & alums