幂等函数与纯函数相同吗?

发布于 2024-10-14 04:16:22 字数 98 浏览 9 评论 0原文

我读了维基百科对幂等性的解释。 我知道这意味着函数的输出由其输入决定。 但我记得我听说过一个非常相似的概念:纯函数。 我用谷歌搜索但找不到它们的区别...

它们相等吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

九公里浅绿 2024-10-21 04:16:22

幂等函数可能会导致幂等副作用。

纯函数不能。

例如,设置文本框文本的函数是幂等的(因为多次调用将显示相同的文本),但不是纯粹的。
同样,按 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)

浅沫记忆 2024-10-21 04:16:22

函数是没有副作用的函数,其输出完全由输入决定 - 也就是说,调用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 as f(x).

A function can be pure, idempotent, both, or neither.

⒈起吃苦の倖褔 2024-10-21 04:16:22

不,幂等函数将更改程序/对象/机器状态 - 并且只会进行一次更改(尽管重复调用)。纯函数不会改变任何内容,并且每次调用时都会继续提供(返回)结果。

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.

绾颜 2024-10-21 04:16:22

功能纯度意味着没有副作用。另一方面,幂等性意味着函数对于多次调用是不变的。

每个纯函数都是副作用幂等的,因为纯函数即使被多次调用也不会产生副作用。然而,返回值幂等性意味着 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.

菩提树下叶撕阳。 2024-10-21 04:16:22

造成混乱的一个重要原因是,在计算机科学中,命令式编程和函数式编程中的幂等性似乎有不同的定义。

来自维基百科(https://en.wikipedia.org/wiki/Idempotence#Computer_science_meaning

在计算机科学中,幂等一词更广泛地用于描述执行一次或多次将产生相同结果的操作。根据应用的上下文,这可能具有不同的含义。例如,在具有副作用的方法或子例程调用的情况下,这意味着修改后的状态在第一次调用后保持不变。不过,在函数式编程中,幂等函数对于任何值 x 都具有 f(f(x)) = f(x) 属性。

由于纯函数不会产生副作用,因此我认为幂等性与纯度无关。

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)

In computer science, the term idempotent is used more comprehensively to describe an operation that will produce the same results if executed once or multiple times. This may have a different meaning depending on the context in which it is applied. In the case of methods or subroutine calls with side effects, for instance, it means that the modified state remains the same after the first call. In functional programming, though, an idempotent function is one that has the property f(f(x)) = f(x) for any value x.

Since a pure function does not produce side effects, i am of the opinion that idempotence has nothing to do with purity.

故事↓在人 2024-10-21 04:16:22

定义:

  • 纯:对于给定的 x,f(x) 始终返回相同的值。
  • 幂等:f(f(x)) = f(x)

示例

非纯粹且非幂等

def f(x):
    return random()

检查:

f(0) = 0.2171
f(0) = 0.3142       # Thus, impure.

纯粹,但非幂等

def f(x):
    return x + 1

检查:

f(0) = 1
f(0) = 1  # Thus, pure.
f(1) = 2  # Thus, not idempotent since f(0) != f(f(0)).

纯粹且幂等

def f(x):
    return round(x)

检查:

f(0.3142) = 0
f(0.3142) = 0  # Thus, pure.
f(0) = 0       # Thus, idempotent.

可视化

< 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 = f(a) = f(f(x)) = f(b) = b

...所以,a = b。我们已经达到了矛盾的地步。显然,f 不能同时是不纯的和幂等的!

Definitions:

  • Pure: f(x) always returns the same value for a given x.
  • Idempotent: f(f(x)) = f(x).

Examples

Not pure, and not idempotent

def f(x):
    return random()

Check:

f(0) = 0.2171
f(0) = 0.3142       # Thus, impure.

Pure, but not idempotent

def f(x):
    return x + 1

Check:

f(0) = 1
f(0) = 1  # Thus, pure.
f(1) = 2  # Thus, not idempotent since f(0) != f(f(0)).

Pure, and idempotent

def f(x):
    return round(x)

Check:

f(0.3142) = 0
f(0.3142) = 0  # Thus, pure.
f(0) = 0       # Thus, idempotent.

Visualization

graph diagrams

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:

a = f(a) = f(f(x)) = f(b) = b

...so, a = b. We have reached a contradiction. Clearly, f cannot be simultaneously impure and idempotent!

初熏 2024-10-21 04:16:22

我发现更多地方将“幂等”定义为 f(f(x)) = f(x) ,但我真的不认为这是准确的。
相反,我认为这个定义更准确(但不完全准确):

描述一个动作,当在同一个动作上执行多次时
对象,在第一次之后对其对象不再有进一步的影响
被执行。投影算子是幂等的。

我解释这一点的方式是,如果我们将 f 应用于 x (主题)两次,如下所示:

f(x);f(x);

那么(副作用)效果与

f(x);

因为纯函数不允许副作用,所以纯函数也是“幂等”的。


幂等的更一般(更准确)的定义还包括类似的函数

切换(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):

describing an action which, when performed multiple times on the same
subject, has no further effect on its subject after the first time it
is performed. A projection operator is idempotent.

The way I interpret this, if we apply f on x (the subject) twice like:

f(x);f(x);

then the (side-)effect is the same as

f(x);

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 like

toggle(x)

We can say the degree of idempotency of a toggle is 2, because after applying toggle every 2 times we always end up with the same State

尐籹人 2024-10-21 04:16:22

正如其他人所说,“幂等”一词具有多种不同的含义。

在纯函数上下文中,幂等意味着 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 that f(f(x)) == f(x) may evaluate to True for all x values, despite that the overall effect of f(f(x)) may be different to the overall effect of f(x), making f(x) and f(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 that f is pure if and only if side_effect[f(x)] == side_effect[]. From this we see that a pure function is automatically idempotent in the final sense of the word.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文