Reputation: 131
Is every pure function idempotent?
I wouldn't ask such a crazy question if I hadn't seen this statement in the official Angular.js tutorial:
The filter function should be a pure function, which means that it should be stateless and idempotent. Angular relies on these properties and executes the filter only when the inputs to the function change.
This seems to imply that a pure function should be both stateless and idempotent, which does not match what I think is the common definition of a pure function.
In fact, it does not even match the example below in the page, where reversing the characters in a string is presented an example of a filter: clearly, reversing a string changes the string in a way that it changes again if the string is reversed once more.
Even more curious: if you look at the Wikipedia page for pure function linked in that tutorial (I can only post one link because I'm a new user, sorry...): it reports sin(x) as an example of a pure function.
So, according to Angular.js, the sine is an idempotence, right?
What am I missing?
Upvotes: 4
Views: 1575
Reputation: 1929
I don't think that f(f(x)) = f(x) is the definition of idempotence, and this definition is functionally not useful since (as noted) the function can never return anything, or always the same return value. I think idempotence as no matter how many times you apply identical inputs, you always get the same outputs. So:
f(x) = y, no matter how many times you call f(x) for any x.
Examples:
f(x) = { return x+1 } => idempotent
f(x) = { return counter++ } (where "counter" is some retained state variable) => not idempotent.
Upvotes: 0
Reputation: 9876
Scrap the original answer, the answer seems to be no. See the comments!
Only if the pure function returns f(f(x)) === f(x)
, which is the case only if the function returns nothing. A good example given is double(x)
, and it's kinda obvious double(double(x)) !== double(x)
Yes. A pure function is always idempotent. However, given the definition of a pure function, it doesn't really makes sense to discuss their idempotent qualities.
Pure functions meet two criteria:
Idempotency is the quality of the function mutating a system state to the same state as after the first call, regardless of how many times that function is called.
E.g., if a bank has a transaction with an ID of x which is processed by the bank's transaction processing system using function processTransaction(id)
, then it should only mutate the state of the system to reflect the transaction once and once only regardless of how many times the system processes it (say, if it was erroneously called twice).
So, given that a pure function doesn't affect the state of a system (no side effects), it will always have no effect on the state of the system. So, it is idempotent as no matter how many times it is called, it will mutate state to the same state as it did after its first call. That first call also won't mutate system state too, just to clarify.
This quality may be lost if you process the result of a pure function with a function that isn't pure.
Upvotes: 2