Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
happytomatoe opened this issue Jul 2, 2023 · 2 comments

Comments

@happytomatoe
Copy link

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
Copy link

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants