Chapter 3 why decrement hp is a pure function if it modifies input argument

#643 · open · 2 comments

View on GitHub ↗

happytomatoe

Why decrement hp is a pure function if it modifies input argument - `const decrementHP = p => p.set('hp', p.get('hp') - 1);` For it to be pure shouldn't it at least copy the map content before changing the value? If you run this in multithreaded env you will get a lot of troubles.

Comments

labriola

The code does not actually indicate that it mutates p. It could very well simply return a new 'p' with the value changed, leaving the original unchanged.

daksh019

this example here: https://www.npmjs.com/package/immutable `const { Map } = require('immutable'); const map1 = Map({ a: 1, b: 2, c: 3 }); const map2 = map1.set('b', 50); map1.get('b') + ' vs. ' + map2.get('b'); // 2 vs. 50 ` illustrates that the data was never mutated.