Skip to content

Instantly share code, notes, and snippets.

@nasturtus
Created April 17, 2017 23:20
Show Gist options
  • Save nasturtus/a087c202760d1dba5da2214ef26906da to your computer and use it in GitHub Desktop.
Save nasturtus/a087c202760d1dba5da2214ef26906da to your computer and use it in GitHub Desktop.
# Illustrating idemptotence
# An operation is idempotent if f(x) => n, f(f(x)) => n, f(f(f(x))) => n ..
# Reference:
# Corey Schafer https://www.youtube.com/watch?v=UaKZ4wKytcA
# The following is not idempotent since the results from square(n)
# and square(square(n)) differ.
def square(n):
return n * n
print(square(10))
print(square(square(10)))
# The following is idempotent.
print(abs(-100))
print(abs(abs(-100)))
print(abs(abs(abs(-100))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment