Skip to content

Instantly share code, notes, and snippets.

@kulte
Created April 2, 2016 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kulte/7c44300d85d6c9b4f3446f8b23022b75 to your computer and use it in GitHub Desktop.
Save kulte/7c44300d85d6c9b4f3446f8b23022b75 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