Skip to content

Instantly share code, notes, and snippets.

@steveosoule
Forked from kulte/pure-impure-1.js
Created April 4, 2016 20:27
Show Gist options
  • Save steveosoule/e9aabdc264fe6510ca382c8668474f59 to your computer and use it in GitHub Desktop.
Save steveosoule/e9aabdc264fe6510ca382c8668474f59 to your computer and use it in GitHub Desktop.
// Examples of pure functions. No side effects. No mutations.
function square(x) {
return x * x;
}
function squareAll(items) {
return items.map(square);
}
// Examples of impure functions.
function square(x) {
updateXInDatabase(x); // A side effect. A database is called.
return x * x;
}
function squareAll(items) {
// Values passed in are being overwritten.
for (let i = 0; i < items.length; i++) {
items[i] = square(items[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment