幂等函数与纯函数相同吗?
我读了维基百科对幂等性的解释。 我知道这意味着函数的输出由其输入决定。 但我记得我听说过一个非常相似的概念:纯函数。 我用谷歌搜索但找不到它们的区别...
它们相等吗?
I read Wikipedia's explanation of idempotence.
I know it means a function's output is determined by its input.
But I remember that I heard a very similar concept: pure function.
I Google them but can't find their difference...
Are they equivalent?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
幂等函数可能会导致幂等副作用。
纯函数不能。
例如,设置文本框文本的函数是幂等的(因为多次调用将显示相同的文本),但不是纯粹的。
同样,按 GUID(而不是按计数)删除记录是幂等的,因为该行在后续调用后仍保持删除状态。 (额外的调用没有任何作用)
An idempotent function can cause idempotent side-effects.
A pure function cannot.
For example, a function which sets the text of a textbox is idempotent (because multiple calls will display the same text), but not pure.
Similarly, deleting a record by GUID (not by count) is idempotent, because the row stays deleted after subsequent calls. (additional calls do nothing)
纯函数是没有副作用的函数,其输出完全由输入决定 - 也就是说,调用
f(x)
将无论调用多少次,都会得到相同的结果。幂等函数是可以多次应用而不改变结果的函数 - 也就是说,f(f(x)) 与 f(x) 相同)。
函数可以是纯函数、幂等函数、两者兼有或两者都不是。
A pure function is a function without side-effects where the output is solely determined by the input - that is, calling
f(x)
will give the same result no matter how many times you call it.An idempotent function is one that can be applied multiple times without changing the result - that is,
f(f(x))
is the same asf(x)
.A function can be pure, idempotent, both, or neither.
不,幂等函数将更改程序/对象/机器状态 - 并且只会进行一次更改(尽管重复调用)。纯函数不会改变任何内容,并且每次调用时都会继续提供(返回)结果。
No, an idempotent function will change program/object/machine state - and will make that change only once (despite repeated calls). A pure function changes nothing, and continues to provide a (return) result each time it is called.
功能纯度意味着没有副作用。另一方面,幂等性意味着函数对于多次调用是不变的。
每个纯函数都是副作用幂等的,因为纯函数即使被多次调用也不会产生副作用。然而,返回值幂等性意味着 f(f(x)) = f(x) 不受纯度影响。
Functional purity means that there are no side effects. On the other hand, idempotence means that a function is invariant with respect to multiple calls.
Every pure function is side effect idempotent because pure functions never produce side effects even if they are called more then once. However, return value idempotence means that f(f(x)) = f(x) which is not effected by purity.
造成混乱的一个重要原因是,在计算机科学中,命令式编程和函数式编程中的幂等性似乎有不同的定义。
来自维基百科(https://en.wikipedia.org/wiki/Idempotence#Computer_science_meaning)
由于纯函数不会产生副作用,因此我认为幂等性与纯度无关。
A large source of confusion is that in computer science, there seems to be different definitions for idempotence in imperative and functional programming.
From wikipedia (https://en.wikipedia.org/wiki/Idempotence#Computer_science_meaning)
Since a pure function does not produce side effects, i am of the opinion that idempotence has nothing to do with purity.
定义:
f(x)
始终返回相同的值。f(f(x)) = f(x)
。示例
非纯粹且非幂等
检查:
纯粹,但非幂等
检查:
纯粹且幂等
检查:
可视化
< img src="https://i.sstatic.net/Aro80.png" alt="图表">
每个箭头表示
f
的一个应用。请注意,在应用一次
f
后,幂等图以 1 个周期结束。那么“不是纯粹的,而是幂等的”呢?
不可能的。反证法:
假设 f 是不纯且幂等的。不纯 f 意味着存在 x,如果 f(x) = a 且 f(x) = b,则有可能 a != b。说发生这种事。现在,根据幂等性,f(a) = a 且 f(b) = b。但然后:
...所以,a = b。我们已经达到了矛盾的地步。显然,f 不能同时是不纯的和幂等的!
Definitions:
f(x)
always returns the same value for a givenx
.f(f(x)) = f(x)
.Examples
Not pure, and not idempotent
Check:
Pure, but not idempotent
Check:
Pure, and idempotent
Check:
Visualization
Each arrow denotes an application of
f
.Notice that the idempotent graph ends up in a 1-cycle after one application of
f
.What about "not pure, but idempotent"?
Impossible. Proof by contradiction:
Assume f is impure and idempotent. Impure f implies there exists x such that if f(x) = a and f(x) = b, then it is possible that a != b. Say that happens. Now, by idempotency, f(a) = a and f(b) = b. But then:
...so, a = b. We have reached a contradiction. Clearly, f cannot be simultaneously impure and idempotent!
我发现更多地方将“幂等”定义为 f(f(x)) = f(x) ,但我真的不认为这是准确的。
相反,我认为这个定义更准确(但不完全准确):
我解释这一点的方式是,如果我们将
f
应用于x
(主题)两次,如下所示:那么(副作用)效果与
因为纯函数不允许副作用,所以纯函数也是“幂等”的。
幂等的更一般(更准确)的定义还包括类似的函数
幂等性的
度
是2,因为每2次应用toggle
后我们总是会得到相同的状态
I've found more places where 'idempotent' is defined as
f(f(x)) = f(x)
but I really don't believe that's accurate.Instead I think this definition is more accurate (but not totally):
The way I interpret this, if we apply
f
onx
(the subject) twice like:then the (side-)effect is the same as
Because pure functions dont allow side-effects then pure functions are trivially also 'idempotent'.
A more general (and more accurate) definition of
idempotent
also includes functions likeWe can say the
degree
ofidempotency
of a toggle is 2, because after applyingtoggle
every 2 times we always end up with the sameState
正如其他人所说,“幂等”一词具有多种不同的含义。
在纯函数上下文中,幂等意味着
f(f(x)) == f(x)
。但在不纯的上下文中,这会分为两个不同的条件,因为f(f(x)) == f(x)
可能会计算为True
所有x
值,尽管f(f(x))
的整体效果可能与f(x)
的整体效果不同,从编译角度来看,f(x)
和f(f(x))
不可互换。雪上加霜的是,幂等还可能意味着 side_effect[f(x); f(x)] == side_effect[f(x)] 。此外,如果满足这个条件,则不会发生上一段中描述的定义分歧。请注意,当且仅当
side_effect[f(x)] == side_effect[]
时,f
才是纯的。由此我们可以看出,纯函数在最终意义上自动具有幂等性。As others have said, the word "idempotent" has multiple distinct meanings.
In a pure-functional context, idempotent means
f(f(x)) == f(x)
. But in an impure context, this bifurcates into two different conditions, due to the fact thatf(f(x)) == f(x)
may evaluate toTrue
for allx
values, despite that the overall effect off(f(x))
may be different to the overall effect off(x)
, makingf(x)
andf(f(x))
non-interchangeable from a compilation perspective.To add insult to injury, idempotent can also mean
side_effect[f(x); f(x)] == side_effect[f(x)]
. Furthermore, if this condition is satisfied, then the definitional bifurcation described in the previous paragraph doesn't occur. Note thatf
is pure if and only ifside_effect[f(x)] == side_effect[]
. From this we see that a pure function is automatically idempotent in the final sense of the word.