Skip to content

Instantly share code, notes, and snippets.

@calvintychan
Created December 4, 2016 22:39
Show Gist options
  • Save calvintychan/079910b7e686a8056c8af0f9dff50217 to your computer and use it in GitHub Desktop.
Save calvintychan/079910b7e686a8056c8af0f9dff50217 to your computer and use it in GitHub Desktop.
Understanding what pure functions are
// https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-pure-function-d1c076bec976#.8n6883iyc
// Definition of a pure function
// Given the same inputs, a pure function will always return the same output,
// regardless of the number of times the function is called.
// Memorization and pure function goes well togther. Since pure function always return a predicatable value,
// this value can be cache using memorization.
// Non Pure
var z;
function add(x, y) {
return x + y + z;
}
// Pure
function add(x, y) {
return x + y;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment