Tuesday, May 7, 2013

What is an idempotent operation?


In computing, an idempotent operation is one that has no additional effect if it is called more than once with the same input parameters.
Pure functions (those whose return value only depends on the input parameters) are always idempotent (Python syntax examples):
def sq(x):
    return x * x
Functions that only set a value are idempotent:
g_x = 5

def set_x(x):
    global g_x
    g_x = x
However, functions that do something like append to a global list are not idempotent:
a = []

def add_x(x):
    a.append(x)
Idempotent operations are often used in the design of network protocols, where a request to perform an operation is guaranteed to happen at least once, but might also happen more than once. If the operation is idempotent, then there is no harm in performing the operation two or more times.
It is somewhat unfortunate that in mathematics, the use of idempotent is slightly different. See the Wikipedia article on idempotence for more information.

No comments:

Post a Comment