Last active
September 1, 2020 19:42
-
-
Save Delni/600a4beb0109da0043df15ab3f8d50e2 to your computer and use it in GitHub Desktop.
Example of pure functions
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
let x: number = 0 | |
// Fonction pure: ne modifie pas x | |
function add(value: number) { | |
return x + value | |
} | |
// Fonction impure: modifie x | |
function incrementBy(value: number) { | |
x += value; | |
return x; | |
} | |
console.log(add(2)) // 2 | |
console.log(add(2)) // 2 | |
console.log(incrementBy(2)) // 2 | |
console.log(incrementBy(2)) // 4 | |
console.log(incrementBy(incrementBy(incrementBy(2)))) // Que vaux x ? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment