On Github 25th-floor / immutablejs-and-react
S. Oestreicher @thinkfunctional A. Sharif @sharifsbeat
var foo = 'Some String'; var bar = foo.substr(1, 3);
JavaScript Strings are immutable.
function top(n, xs) { xs.sort().reverse(); return xs.slice(0, n); } // somewhere else let xs = [4, 1, 3, 2, 5]; let top3 = top(3, xs); // oops, xs has been changed! // Do I need to make a copy? Should "top" make a copy?
let now = moment(); let startOfWeek = now.startOf('week'); let endOfWeek = now.endOf('week'); now.format('YYYY-MM-DD'); // => 2015-11-07 startOfWeek.format('YYYY-MM-DD'); // => 2015-11-07 endOfWeek.format('YYYY-MM-DD'); // => 2015-11-07 // we have to remember to make a copy every time! let now = moment(); let startOfWeek = now.clone().startOf('week'); let endOfWeek = now.clone().endOf('week');
"Purely functional tree, after" by VineetKumar at English Wikipedia
Immutable.fromJS([{a:1}, {b:2}])get()
let map = Immutable.fromJS({foo: {bar: 1}}); map.get('foo').get('bar'); //instead of map.foo.barImmutable.is()
Immutable.is(this.props.cols, nextProps.cols);
rows.updateIn([row, 'cols', col, 'active'], active => !active);setIn()
rows.setIn([row, 'cols', col, 'active'], false);mergeDeep()
var x = Immutable.fromJS({a: { x: 10, y: 10 }, b: { x: 20, y: 50 } }); var y = Immutable.fromJS({a: { x: 2 }, b: { y: 5 }, c: { z: 3 } }); x.mergeDeep(y) // {a: { x: 2, y: 10 }, b: { x: 20, y: 5 }, c: { z: 3 } }
The secret sauce of why ClojureScript React wrappers are faster than vanilla React.
Useful, but you should prefer your own implementation to leverage Immutable.is
var a = Immutable.List([1,2]); var b = Immutable.List([1,2]); a === b; // => false Immutable.is(a, b); // => true
var map = Immutable.Map(); map = map.set(Immutable.Map({foo:'bar'}), 'foobar'); map.get(Immutable.Map({foo:'bar'})); // => 'foobar' map.toJSON(); // => {"[object Object]": "foobar"} // sad :-(
function handle(what) { return transit.makeWriteHandler({ tag: v => what, rep: v => v, stringRep: v => null }); } var writer = transit.writer("json-verbose", { handlers: transit.map([ Immutable.List, handle('array'), Immutable.Map, handle('map') ]) });
var reader = transit.reader("json", { arrayBuilder: { init: function(node) { return Immutable.List().asMutable(); }, add: function(ret, val, node) { return ret.push(val); }, finalize: function(ret, node) { return ret.asImmutable(); }, fromArray: function(arr, node) { return Immutable.List.from(arr); } }, mapBuilder: { init: function(node) { return Immutable.Map().asMutable(); }, add: function(ret, key, val, node) { return ret.set(key, val); }, finalize: function(ret, node) { return ret.asImmutable(); } } });
Demo Time!