Skip to content

Instantly share code, notes, and snippets.

@Delni
Last active September 1, 2020 19:42
Show Gist options
  • Save Delni/600a4beb0109da0043df15ab3f8d50e2 to your computer and use it in GitHub Desktop.
Save Delni/600a4beb0109da0043df15ab3f8d50e2 to your computer and use it in GitHub Desktop.
Example of pure functions
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