5 ms·
No. A function which multiplies a number by two is side effect free, but is no idempotent.
by someonewithpc 8y ago
No. A function which multiplies a number by two is side effect free, but is no idempotent.
- amelius 8y agoExcept if the domain is zero.
- falcor84 8y agoOr even a slightly more exotic case, such as Z mod 2 (a single binary digit)
- deleted 8y ago[deleted]
- mseebach 8y agoI think that is considered idempotent in the REST-sense of the word: you can multiply a number by two as many times as you like, the result will be the same (and no state is mutated). I looked it up, apparently there's a formal definition "denoting an element of a set which is unchanged in value when multiplied or otherwise operated on by itself", which does not seem to describe the REST usage very well, though.
- laneb 8y agoThe typical operation applied to sets of functions is composition, so idempotency of a function f is the condition that f(f(x)) = f(x) for all x in the domain of f. I don't think that applies meaningfully to GET.
- jholman 8y agoIt does apply to HTTP idempotency. `x` is the state of the server. `f` is the change to the state of the server that ensues when one makes such-and-such an HTTP call. So taking PUT as an example, `x` is the state before the PUT, `f(x)` is the state after one PUT, and `f(f(x))` is the state after that single PUT is sent twice. Of course in a RFC7231-compliant server, `f(x) = f(f(x))`. Taking GET (or any other nullipotent method) as an example, we also see that in a RFC7231-compliant server, `x = f(x) = f(f(x))`.
- laneb 8y agoAh yeah that makes sense.
- CapacitorSet 8y agoTechnically speaking, idempotency as defined by RFC7231 only requires f(x) = f(f(x)).
- jholman 8y agoYes, and nullipotency requires x = f(x) = f(f(x)), and four of the methods defined in RFC7231 are expected to be nullipotent, otherwise known as "safe". The point of mentioning that is to highlight the relationship between idempotence and nullipotence.
- StavrosK 8y agoI dispute your example. If you call f(2) and it always returns 4, it's idempotent and side-effect-free. If you call f() and it returns 4, then 8, etc, it is neither.
- mbid 8y agoFrom wikipedia: "A unary operation f, that is, a map from some set S into itself, is called idempotent if, for all x in S, f(f(x)) = f(x)."
- StavrosK 8y agoYes, in mathematics, not programming. And a function that doubles a number isn't idempotent even by that definition.
- Y_Y 8y agoOf course a doubling function is not idempotent! I think the confusion arises because side-effectful functions can be considered as having type f :: (RealWorld, OtherArgs) -> (RealWorld, OtherOutputs) and so for a garage door toggle you have something like t :: RealWorld -> RealWorld where the new state is the old one with the door opened/closed as appropriate. Now the idempotence condition becomes: t(t(world)) == t(world) but clearly t(t(doorOpenWorld) = t(doorClosedWorld) = doorOpenWorld != t(doorOpenWorld) = doorclosedWorld so this is where the notion comes from. If you abuse notation and just say a function of no arguments can be idempotent then you'll get confusion like this.
- smaddox 8y agoBut `GET(GET(x))` doesn't make sense, in general (and if it did, then you would not expect it to be idempotent), so clearly idempotency in this context is meant to mean side-effect free. They should probably just say side-effect free, though, to avoid the confusion.
- 8y ago