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

Functional Programming: What? Why? How?

Functional Programming: What? Why? How?

Presented at HolyJS 2017 (https://holyjs-piter.ru/en/talks/functional-programming-in-js-what-why-how/)

Functional programming has become a hot topic in the JS community, leaving many JavaScripters wondering: What's all the hype about? What do functional programs look like, and why might I prefer them to imperative/object-oriented code? How do I get started writing code in a functional style?

If you've been asking yourself these questions, you'll enjoy this gentle, practical intro to functional programming. We'll explore the advantages and basic principles of the paradigm, in which holy, pure functions deliver us from the evils of mutable state. We'll see how rejecting side-effects and mutability in our code can avoid a lot of common JS headaches. And we'll learn how to use core JS features as well as some popular FP libraries like Mori and Ramda to start writing our own functional programs.

Anjana Sofia Vakil

June 03, 2017
Tweet

More Decks by Anjana Sofia Vakil

Other Decks in Programming

Transcript

  1. pure functor side effect monad monoid compose curry λ immutable

    stateless lazy referential transparency higher-order
  2. pure functor side effect monad monoid compose curry λ immutable

    stateless lazy referential transparency higher-order ???
  3. Imperative follow my commands do this, then that Object-Oriented keep

    state to yourself send/receive messages Declarative this is what I want I don't care how you do it Functional ???
  4. Not pure: var name = "Saint Petersburg"; function greet() {

    console.log("Hello, " + name + "!"); } greet(); // Hello, Saint Petersburg! name = "HolyJS"; greet(); // Hello, HolyJS!
  5. Pure: function greet(name) { return "Hello, " + name +

    "!"; } greet("Saint Petersburg"); // "Hello, Saint Petersburg!" greet("HolyJS"); // "Hello, HolyJS!"
  6. Imperative: var city = "St. Petersburg"; var greeting = "Hi";

    console.log(greeting + ", " + city + "!"); // Hi, St. Petersburg! greeting = "Привет"; console.log(greeting + ", " + city + "!"); // Привет, St. Petersburg!
  7. Functional: function greet(greeting, name) { return greeting + ", "

    + name + "!"; } greet("Hi", "St. Petersburg"); // "Hi, St. Petersburg!" greet("Привет", "HolyJS"); // "Привет, HolyJS!"
  8. Side effects: var conf = {name: "SaintJS", date: 2017}; function

    renameConf(newName) { conf.name = newName; console.log("Renamed!"); } renameConf("HolyJS"); // Renamed! conf; // {name: "HolyJS", date: 2017}
  9. No side effects: var conf = {name: "SaintJS", date: 2017};

    function renameConf(oldConf, newName) { return {name: newName, date: oldConf.date} } var newConf = renameConf(conf, "HolyJS"); newConf; // {name: "HolyJS", date: 2017} conf; // {name: "SaintJS", date: 2017}
  10. Mutation (dangerous!): var numSysts = ["Roman", "Arabic", "Chinese"]; numSysts[1] =

    "Hindu-" + numSysts[1]; numSysts; // ["Roman", "Hindu-Arabic", "Chinese"]
  11. No mutation (safer!): const numSysts = ["Roman", "Arabic", "Chinese"]; const

    newNumSysts = numSysts.map((num) => { if (num === "Arabic") { return "Hindu-" + num; } else { return num; } }); newNumSysts;//["Roman", "Hindu-Arabic", "Chinese"] numSysts; //["Roman", "Arabic", "Chinese"]
  12. 0 1 2 3 4 5 6 7 foo 8

    1 2 3 4 5 6 7 too Immutable data
  13. 0 1 2 3 4 5 6 7 foo 8

    1 2 3 4 5 6 7 too Immutable data (inefficient)
  14. 0 1 2 3 foo 4 5 6 7 1

    8 too Immutable data (efficient)
  15. 0 1 2 3 foo 4 5 6 7 1

    8 too Persistent (immutable) data structures structural sharing
  16. Mori https://swannodette.github.io/mori var f = mori.vector(1,2); var t = mori.conj(f,

    3); • ClojureScript port • Functional API • Fast Immutable.js https://facebook.github.io/immutable-js var f = Immutable.List.of(1,2); var t = f.push(3); • JS through & through • Public methods • A bit smaller than Mori Persistent data structures
  17. Iteration: function sum (numbers) { let total = 0; for

    (i = 0; i < numbers.length; i++) { total += numbers[i]; } return total; } sum([0,1,2,3,4]); // 10
  18. Recursion: function sum (numbers) { if (numbers.length === 1) {

    return numbers[0]; } else { return numbers[0] + sum(numbers.slice(1)); } } sum([0,1,2,3,4]); // 10
  19. Closures: function makeAdjectifier(adjective) { return function (noun) { return adjective

    + " " + noun; }; } var holify = makeAdjectifier("holy"); holify("JS"); // "holy JS" holify("cow"); // "holy cow"
  20. Outputs become next inputs: var ender = (ending) => (input)

    => input + ending; var adore = ender(" rocks"); var announce = ender(", y'all"); var exclaim = ender("!"); var hypeUp = (x) => exclaim(announce(adore(x))); hypeUp("JS"); // "JS rocks, y'all!" hypeUp("FP"); // "FP rocks, y'all!"
  21. Function composition/pipelining: var r = require("ramda"); var rtlHype = r.compose(adore,

    announce, exclaim); rtlHype("FP"); // "FP!, y'all rocks" var ltrHype = r.pipe(adore, announce, exclaim); ltrHype("FP"); // "FP rocks, y'all!"
  22. FP libraries for JS • Mori (swannodette.github.io/mori) • Immutable.js (facebook.github.io/immutable-js)

    • Ramda (ramdajs.com) • Underscore (underscorejs.org) • Lodash (lodash.com) • ...and more!
  23. Further reading/watching Code Words by the Recurse Center (codewords.recurse.com) •

    Mary Rose Cook, "An introduction to functional programming" • Sal Becker, "This just isn't functional" • Patrick Dubroy, "Immutability is not enough" J. Kerr, “Functional Principles for OO Development”, GOTO Chicago '14 youtu.be/GpXsQ-NIKXY Me, "Immutable data structures for functional JS", JSConf EU '17 youtu.be/Wo0qiGPSV-s D. Nolen, “Immutability, interactivity & JS”, FutureJS '14 youtu.be/mS264h8KGwk L. Byron, “Immutable Data & React”, React.js Conf '15 youtu.be/I7IdS-PbEgI B. Stokke, "The Miracle of Generators", GOTO Chicago '16 youtu.be/6mCkLZ0cwAI
  24. Thanks for listening! I’m @AnjanaVakil Huge thanks to: Mary Rose

    Cook, Sal Becker, Khalid Ali & The Recurse Center HolyJS organizers