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

Graph Theory behind ImmutableJS

Graph Theory behind ImmutableJS

ImmutableJS is a library from Facebook which provides us a set of immutable data structures, such as Stack, List Map and others. With this you can build predictable and reliable state models on top of your application, making it easier to handle state.

These structures are highly efficient on modern Javascript VMS by using structural sharing via hash maps tries and vector tries. But how they do this?

The point is: there is a lot of graph and algorithm theory behind this. Direct Acyclic graphs and complexity of algorithms.

There is a difference between implementation and interface. On this talk we will have these two visions.

At the end of this talk, you should understand how the things really work behind the scenes!

Franzé Jr.

October 04, 2016
Tweet

More Decks by Franzé Jr.

Other Decks in Programming

Transcript

  1. “Good developers know how things work. Great developers know why

    things work.” — Steve Souders,(O’Reilly Media, 2013).
  2. MAP

  3. REACT WAS CREATED IN 2012. LEE BYRON NEEDED TO CONVINCE

    JORDAN WALKE THAT REACT WAS A BAD IDEA. FOR SOME REASONS...
  4. Coming back to Virtual DOM > Whenever anything may have

    changed, re-render everything to a virtual DOM representation
  5. MANY PRACTICAL PROBLEMS CAN BE REPRESENTED BY GRAPHS. MODEL MANY

    TYPES OF RELATIONS AND PROCESSES IN PHYSICAL, BIOLOGICAL, SOCIAL AND INFORMATION SYSTEMS.
  6. A general trie [10, 13] is a lookup structure for

    finite strings that acts like a Deterministic Finite Automaton (DFA) without any loops: the transitions are the characters of the strings, the internal nodes encode prefix sharing, and the accept nodes may point to values associated with the strings
  7. LIST import { List } from "immutable" var list =

    List([1,2,3]) var list2 = list.push(4)
  8. LIST import { List } from "immutable" var list =

    List([1,2,3]) var list2 = list.push(4) list // [1,2,3] list2 === list // false
  9. LIST import { List } from "immutable" var list =

    List([1,2,3]) var list2 = list.push(4) list // [1,2,3] list2 === list // false console.log(list.toString()) // List [ 1, 2, 3 ] console.log(list2.toString()) // List [ 1, 2, 3, 4 ]
  10. MAP var Immutable = require('immutable'); var map1 = Immutable.Map({a:1, b:2,

    c:3}); var map2 = map1.set('b', 50); map1.get('b'); // 2 map2.get('b'); // 50
  11. Accepts raw Javascript objects var map1 = Immutable.Map({a:1, b:2, c:3,

    d:4}); var map2 = Immutable.Map({c:10, a:20, t:30}); var obj = {d:100, o:200, g:300}; var map3 = map1.merge(map2, obj); // Map { a: 20, b: 2, c: 10, d: 100, t: 30, o: 200, g: 300 }
  12. Converts back to raw JavaScript objects. var deep = Immutable.Map({

    a: 1, b: 2, c: Immutable.List.of(3, 4, 5) }); deep.toObject() // { a: 1, b: 2, c: List [ 3, 4, 5 ] } deep.toArray() // [ 1, 2, List [ 3, 4, 5 ] ] deep.toJS() // { a: 1, b: 2, c: [ 3, 4, 5 ] } JSON.stringify(deep) // '{"a":1,"b":2,"c":[3,4,5]}'
  13. friend = person.put(:name, "James") # => Hamster::Hash[:name => "James", :gender

    => :male] person # => Hamster::Hash[:name => "Simon", :gender => :male] friend[:name] # => "James" person[:name] # => "Simon"
  14. Pete Hunt - The Secrets of React's Virtual DOM (FutureJS

    2014) https://www.youtube.com/watch?v=-DX3vJiqxm4
  15. Episode 8 - React, GraphQL, Immutable & Bow-Ties with Special

    Guest Lee Byron Thursday 31 December 2015 > Link > reactpodcast.com