Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
722 views
in Technique[技术] by (71.8m points)

language-agnostic - 什么是幂等运算?(What is an idempotent operation?)

什么是幂等运算?

  ask by Will translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In computing, an idempotent operation is one that has no additional effect if it is called more than once with the same input parameters.

(在计算中,幂等运算是指如果使用相同的输入参数多次调用幂等运算,则不会产生任何其他影响。)

For example, removing an item from a set can be considered an idempotent operation on the set.

(例如,从集合中删除项目可以视为对该集合的幂等操作。)

In mathematics, an idempotent operation is one where f(f(x)) = f(x) .

(在数学中,幂等运算是f(f(x))= f(x)的情况 。)

For example, the abs() function is idempotent because abs(abs(x)) = abs(x) for all x .

(例如, abs()函数是幂等的,因为对于所有x abs(abs(x)) = abs(x) 。)

These slightly different definitions can be reconciled by considering that x in the mathematical definition represents the state of an object, and f is an operation that may mutate that object.

(通过考虑数学定义中的x表示对象的状态,并且f是可以使该对象变异的运算,可以调和这些稍有不同的定义。)

For example, consider the Python set and its discard method.

(例如,考虑Python set及其discard方法。)

The discard method removes an element from a set, and does nothing if the element does not exist.

(discard方法从集合中删除一个元素,如果该元素不存在,则不执行任何操作。)

So:

(所以:)

my_set.discard(x)

has exactly the same effect as doing the same operation twice:

(与执行两次相同的操作具有完全相同的效果:)

my_set.discard(x)
my_set.discard(x)

Idempotent operations are often used in the design of network protocols, where a request to perform an operation is guaranteed to happen at least once, but might also happen more than once.

(幂等操作通常用于网络协议的设计中,在该协议中,执行一项操作的请求至少会发生一次,但也可能会发生多次。)

If the operation is idempotent, then there is no harm in performing the operation two or more times.

(如果该操作是幂等的,则两次执行此操作不会有任何危害。)

See the Wikipedia article on idempotence for more information.

(有关更多信息,请参见有关幂等性的Wikipedia文章。)


The above answer previously had some incorrect and misleading examples.

(上面的答案以前有一些不正确和误导性的例子。)

Comments below written before April 2014 refer to an older revision.

(以下注释是在2014年4月之前写的,是较旧的版本。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...