Is a pure function idempotent?
Asked Answered
T

2

3

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?

Tessellation answered 8/10, 2015 at 8:36 Comment(2)
#1077912 may have some insightTakara
"clearly, reversing a string changes the string in a way that it changes again if the string is reversed once more" – You take string A, you reverse it and get string B, you reverse string B you get string C (which coincidentally is identical to A). But reversing string A over and over always results in string B. Same input, same output. Different input, different output.Dryasdust
G
2

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:

  • Determinism, i.e., the function results in identical outputs given identical inputs,
  • No side effects, i.e., the function only changes the state of its internal scope.

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.

Gestation answered 24/10, 2017 at 10:47 Comment(8)
This answer seems reasonable, but to improve clarity even further, it would be nice to see different examples of functions (some that return values and some that don't, some that change state and somes that don't), and step-by-step explain why they are pure/impure or idempotent/idempotent. This answer basically says that purity implies idempotence (but is that always true?)Bifarious
@Bifarious Do you know of any examples of pure functions that aren't idempotent?Gestation
Well, I've seen idempotence defined as f(f(x)) = f(x). However, a pure function that always returns the same value given the same input would not satisfy this. This is would only work if f doesn't return anything.Bifarious
Hmm, the classic example would be abs. So I get your definition, abs(abs(-1)) == abs(-1). What I think you're saying is that the lhs has an input of 1 (abs(-1) = 1) and the rhs has an input of -1. So if I've understood that part right, they can satisfy the purity condition as they have different inputs - or am I missing the point there!Gestation
Putting it in yours terms, one has an input x,and the other f(x). Again, I might be missing your pointGestation
Consider the function f = lambda x: x * 2. This a pure function. f(2) = 4 != f(f(2)) = 8.Bifarious
Sorry, that example was in Python, but I hope you understand it.Bifarious
Oh I see! So an idempotent function will return the same value regardless of the different inputs, x and f(x). Read a little into it and that looks right, thanks!Gestation
S
0

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.
Schistosomiasis answered 30/11, 2023 at 18:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.