MostlyAdequate / mostly-adequate-guide

Mostly adequate guide to FP (in javascript)
Other
23.39k stars 1.86k forks source link

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

Open happytomatoe opened 1 year ago

happytomatoe commented 1 year ago

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.

labriola commented 10 months ago

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 commented 8 months ago

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.