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

Immutable Data & Immutable.js

Immutable Data & Immutable.js

What is immutable data, why should I use it, and how do I use it? This talk was originally given during my Senior Phase at Fullstack Academy.

Beth Qiang

January 12, 2017
Tweet

Other Decks in Programming

Transcript

  1. object-oriented programming is an object with attributes and methods defined

    in a class* the main unit of *including fake classes, *cough* javascript *cough*
  2. functional programming is a function that must produce the same

    output for the same input the main unit of
  3. –Michael Feathers “Object oriented programming makes code understandable by encapsulating

    moving parts. Functional programming makes code understandable by minimizing moving parts.”
  4. what will the following block of code print? let string

    = 'magic'; string[4] = 'k'; console.log(string);
  5. however, objects are mutable… let arr1 = [1, 2, 3];

    arr[2] = 5; console.log(arr1); // [ 1, 2, 5 ]
  6. …sorta let arr1 = [1, 2, 3]; let arr2 =

    arr1.map(item => item * 2); console.log(arr1); // [ 1, 2, 3 ] console.log(arr2); // [ 2, 4, 6 ]
  7. const list1 = Immutable.List([1, 2, 3]); const list2 = list1.push(4);

    console.log(list1); // [1, 2, 3] console.log(list2); // [1, 2, 3, 4]
  8. List.prototype.push = function (value) { // 1. make a copy

    var clone = deepCopy(this); // 2. edit the copy clone[clone.length] = value; // 3. return the copy return clone; };
  9. A B C D E F G H directed acyclic

    graph (trie) B’ C’
  10. A B C D E F G H directed acyclic

    graph (trie) B’ C’ D’
  11. const list = Immutable.List(['Batman', 'Spiderman', 'Superman']); // or: const list

    = Immutable.List.of('Batman', 'Spiderman', 'Superman'); list[0]; // undefined list.get(0); // "Batman" const list2 = list.set(0, 'Bruce Wayne’); // list = ["Batman", "Spiderman", "Superman"] // list2 = ["Bruce Wayne", "Spiderman", "Superman"] lists
  12. const list = Immutable.List(['Batman', 'Spiderman', 'Superman']); // or: const list

    = Immutable.List.of('Batman', 'Spiderman', 'Superman'); list[0]; // undefined list.get(0); // "Batman" const list2 = list.set(0, 'Bruce Wayne’); // list = ["Batman", "Spiderman", "Superman"] // list2 = ["Bruce Wayne", "Spiderman", "Superman"] lists
  13. const list = Immutable.List(['Batman', 'Spiderman', 'Superman']); // or: const list

    = Immutable.List.of('Batman', 'Spiderman', 'Superman'); list[0]; // undefined list.get(0); // "Batman" const list2 = list.set(0, 'Bruce Wayne’); // list = ["Batman", "Spiderman", "Superman"] // list2 = ["Bruce Wayne", "Spiderman", "Superman"] lists
  14. maps const map = Immutable.Map({ name: 'Bruce Wayne', city: 'Gotham'

    }); map.name; // undefined map.get(name); // "Bruce Wayne" const map2 = map.set('city', 'outlawed'); // map = {"name": "Bruce Wayne", "city": “Gotham"} // map2 = {"name": "Bruce Wayne", "city": "outlawed"}
  15. maps const map = Immutable.Map({ name: 'Bruce Wayne', city: 'Gotham'

    }); map.name; // undefined map.get('name'); // "Bruce Wayne" const map2 = map.set('city', 'outlawed'); // map = {"name": "Bruce Wayne", "city": “Gotham"} // map2 = {"name": "Bruce Wayne", "city": "outlawed"}
  16. nested objects const nested = Immutable.fromJS({ dogs: [ { name:

    'Fido', favFood: 'chicken' }, { name: 'Max', favFood: 'salmon' } ] }); const nested = Immutable.Map({ dogs: Immutable.List([ Immutable.Map({ name: 'Fido', favFood: 'chicken' }), Immutable.Map({ name: 'Max', favFood: 'salmon' }) ]) });
  17. const nested = Immutable.fromJS({ dogs: [ { name: 'Fido', favFood:

    'chicken' }, { name: 'Max', favFood: 'salmon' } ] }); nested.get('dogs').get(1).get('favFood'); nested.getIn(['dogs', 1, ‘favFood’]); // "salmon" nested objects
  18. const nested = Immutable.fromJS({ dogs: [ { name: 'Fido', favFood:

    'chicken' }, { name: 'Max', favFood: 'salmon' } ] }); nested.get('dogs').get(0).set('favFood', 'pizza'); nested.setIn(['dogs', 0, 'favFood'], ‘pizza'); // {"name": "Fido", "favFood": "pizza"} nested objects
  19. const nested = Immutable.fromJS({ dogs: [ { name: 'Fido', favFood:

    'chicken' }, { name: 'Max', favFood: 'salmon' } ] }); nested.get('dogs') .map(dog => dog.update('favFood’, favFood => favFood.toUpperCase()) ); // [{"name": "Fido", "favFood": "CHICKEN"}, // {"name": "Max", "favFood": "SALMON"}] nested objects
  20. • Official Documentation: not the greatest documentation that’s ever existed.

    • “Unofficial” Documentation: “An Introduction with examples written for humans.” • Immutable REPL: all the goods of Immutable.js without having to set up an environment. • Immutable.js Object Formatter: make Immutable data in the console much less annoying to access and read.