Are idempotent functions the same as pure functions?

FunctionIdempotentPure Function

Function Problem Overview


I read Wikipedia's explanation of idempotence. I know it means a function's output is determined by it's input. But I remember that I heard a very similar concept: pure function. I Google them but can't find their difference...

Are they equivalent?

Function Solutions


Solution 1 - Function

An idempotent function can cause idempotent side-effects.

A pure function cannot.

For example, a function which sets the text of a textbox is idempotent (because multiple calls will display the same text), but not pure.
Similarly, deleting a record by GUID (not by count) is idempotent, because the row stays deleted after subsequent calls. (additional calls do nothing)

Solution 2 - Function

A pure function is a function without side-effects where the output is solely determined by the input - that is, calling f(x) will give the same result no matter how many times you call it.

An idempotent function is one that can be applied multiple times without changing the result - that is, f(f(x)) is the same as f(x).

A function can be pure, idempotent, both, or neither.

Solution 3 - Function

No, an idempotent function will change program/object/machine state - and will make that change only once (despite repeated calls). A pure function changes nothing, and continues to provide a (return) result each time it is called.

Solution 4 - Function

Functional purity means that there are no side effects. On the other hand, idempotence means that a function is invariant with respect to multiple calls.

Every pure function is side effect idempotent because pure functions never produce side effects even if they are called more then once. However, return value idempotence means that f(f(x)) = f(x) which is not effected by purity.

Solution 5 - Function

A large source of confusion is that in computer science, there seems to be different definitions for idempotence in imperative and functional programming.

From wikipedia (https://en.wikipedia.org/wiki/Idempotence#Computer_science_meaning) > In computer science, the term idempotent is used more comprehensively to describe an operation that will produce the same results if executed once or multiple times. This may have a different meaning depending on the context in which it is applied. In the case of methods or subroutine calls with side effects, for instance, it means that the modified state remains the same after the first call. In functional programming, though, an idempotent function is one that has the property f(f(x)) = f(x) for any value x.

Since a pure function does not produce side effects, i am of the opinion that idempotence has nothing to do with purity.

Solution 6 - Function

I've found more places where 'idempotent' is defined as f(f(x)) = f(x) but I really don't believe that's accurate. Instead I think this definition is more accurate (but not totally):

> describing an action which, when performed multiple times on the same > subject, has no further effect on its subject after the first time it > is performed. A projection operator is idempotent.

The way I interpret this, if we apply f on x (the subject) twice like: > f(x);f(x);

then the (side-)effect is the same as > f(x);

Because pure functions dont allow side-effects then pure functions are trivially also 'idempotent'.


A more general (and more accurate) definition of idempotent also includes functions like

> toggle(x)

We can say the degree of idempotency of a toggle is 2, because after applying toggle every 2 times we always end up with the same State

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLai Yu-HsuanView Question on Stackoverflow
Solution 1 - FunctionSLaksView Answer on Stackoverflow
Solution 2 - FunctionAnon.View Answer on Stackoverflow
Solution 3 - FunctionBrent AriasView Answer on Stackoverflow
Solution 4 - FunctionjhuniView Answer on Stackoverflow
Solution 5 - FunctionlohfuView Answer on Stackoverflow
Solution 6 - FunctionIbrahim ben SalahView Answer on Stackoverflow