Created
December 4, 2016 22:39
-
-
Save calvintychan/079910b7e686a8056c8af0f9dff50217 to your computer and use it in GitHub Desktop.
Understanding what pure functions are
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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