Skip to content

Instantly share code, notes, and snippets.

@bhaveshdaswani93
Last active December 30, 2019 03:40
Show Gist options
  • Save bhaveshdaswani93/21bfd0eaca9508938e1befc39bc8d044 to your computer and use it in GitHub Desktop.
Save bhaveshdaswani93/21bfd0eaca9508938e1befc39bc8d044 to your computer and use it in GitHub Desktop.
Let's understand a key term idempotence in functional programming
//here i will try to explain a key concept idempotence with example
function notIdempotenceFn(num) {
return Math.random(num);
}
notIdempotenceFn(5);
notIdempotenceFn(5);
// the output of above two function will be differeht however passing the same input so the function is not idempotent
function idempotentFn(num) {
console.log(num);
}
// The above function is idempotent because every time we call it, it does the same thing for same input so it is idempotent
// it is not pure because it is alter its outside world as console.log will log in window
//Another feature of idempotence is the ability to call itself again and again and still the output is the same let see this with
// an example
function getAbsolute(x) {
return Math.abs(x)
}
getAbsolute(getAbsolute(getAbsolute(-50))) //50
// I am calling getAbsolute function again and again but it output will be the same.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment