什么是幂等运算?
当前回答
任何操作,每n个结果都会产生与第1个结果值匹配的输出。例如,-1的绝对值是1。-1的绝对值的绝对值是1。-1绝对值的绝对值的绝对值等于1。等等。请参见:什么时候使用递归是非常愚蠢的?
其他回答
幂等操作:多次执行没有副作用的操作。 示例:从数据资源检索值并打印值的操作 非幂等操作:多次执行会造成伤害的操作。(当它们改变某些值或状态时) 示例:从银行账户提款的操作
my 5c: In integration and networking the idempotency is very important. Several examples from real-life: Imagine, we deliver data to the target system. Data delivered by a sequence of messages. 1. What would happen if the sequence is mixed in channel? (As network packages always do :) ). If the target system is idempotent, the result will not be different. If the target system depends of the right order in the sequence, we have to implement resequencer on the target site, which would restore the right order. 2. What would happen if there are the message duplicates? If the channel of target system does not acknowledge timely, the source system (or channel itself) usually sends another copy of the message. As a result we can have duplicate message on the target system side. If the target system is idempotent, it takes care of it and result will not be different. If the target system is not idempotent, we have to implement deduplicator on the target system side of the channel.
幂等运算即使调用多次也会产生相同状态的结果,前提是传入相同的参数。
简而言之,幂等运算意味着无论你做多少次幂等运算都不会得到不同的结果。
例如,根据HTTP规范的定义,GET、HEAD、PUT、DELETE是幂等操作;但是POST和PATCH不是。这就是为什么有时POST被PUT取代的原因。
任何操作,每n个结果都会产生与第1个结果值匹配的输出。例如,-1的绝对值是1。-1的绝对值的绝对值是1。-1绝对值的绝对值的绝对值等于1。等等。请参见:什么时候使用递归是非常愚蠢的?