纯函数可以读取全局状态吗?

发布于 2024-07-14 10:12:16 字数 224 浏览 9 评论 0原文

请注意:“纯”函数并不是指“纯虚拟”
我指的是 this

如果函数“读取”某些全局状态,是否会自动呈现它不纯洁? 还是取决于其他因素?

如果它自动使其变得不纯,请解释原因。

如果取决于其他因素,请解释它们是什么。

Please note: by a "pure" function, I don't mean "pure virtual"
I'm referring to this

If a function "reads" some global state, does that automatically render it impure? or does it depend on other factors?

If it automatically renders it impure, please explain why.

If it depends on other factors, please explain what are they.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

鸠魁 2024-07-21 10:12:16

“纯”函数是其结果取决于其输入参数的函数。 如果它读取其他任何内容,则它不是纯函数。

A "pure" function is a function whose result depends only on its input arguments. If it reads anything else, it is not a pure function.

菩提树下叶撕阳。 2024-07-21 10:12:16

在某些特殊情况下,是的。 例如,如果您有一个已计算值的全局缓存,仅由您的函数读取和写入,那么它在数学上仍然是纯的,因为输出仅取决于输入,但它不会是纯的从最严格的意义上来说。 例如:

static int cache[256] = {0};
int compute_something(uint8_t input)
{
    if(cache[input] == 0)
        cache[input] = (perform expensive computation on input that won't return 0);
    return cache[input];
}

在这种情况下,只要没有其他函数接触全局缓存,它仍然是一个数学上的纯函数,即使它在技术上依赖于外部全局状态。 然而,这种状态只是一种性能优化——没有它它仍然会执行相同的计算,只是速度更慢。

In certain specialized instances, yes. For example, if you had a global cache of already-computed values that was only read and written by your function, it would still be mathematically pure, in the sense that the output only depended on the inputs, but it wouldn't be pure in the strictest sense. For example:

static int cache[256] = {0};
int compute_something(uint8_t input)
{
    if(cache[input] == 0)
        cache[input] = (perform expensive computation on input that won't return 0);
    return cache[input];
}

In this case, so long as no other function touches the global cache, it's still a mathematically pure function, even though it technically depends on external global state. However, this state is just a performance optimization -- it would still perform the same computation without it, just more slowly.

臻嫒无言 2024-07-21 10:12:16

构造纯表达式需要纯函数。 根据定义,常量表达式是纯的。

所以,如果你的全球“状态”没有改变,那就没问题。

另请参阅引用透明度

一个更微妙的例子是使用全局变量(或动态作用域变量,或词法闭包)来帮助计算结果的函数。 由于该变量不作为参数传递但可以更改,因此即使参数相同,后续调用该函数的结果也可能不同。 (在纯函数式编程中,不允许破坏性赋值;因此使用全局(或动态作用域)变量的函数仍然是引用透明的,因为这些变量无法更改。)

Pure functions are required to construct pure expressions. Constant expressions are pure by definition.

So, if your global 'state' doesn't change you are fine.

Also see referential transparency:

A more subtle example is that of a function that uses a global variable (or a dynamically scoped variable, or a lexical closure) to help it compute its results. Since this variable is not passed as a parameter but can be altered, the results of subsequent calls to the function can differ even if the parameters are identical. (In pure functional programming, destructive assignment is not allowed; thus a function that uses global (or dynamically scoped) variables is still referentially transparent, since these variables cannot change.)

自由如风 2024-07-21 10:12:16

例如,在 Haskell 中,您可以在不纯的一侧创建一个无限的随机数列表,并将该列表传递给您的纯函数。 仅当需要时,实现才会生成纯函数正在使用的下一个数字,但该函数仍然是纯函数。

In Haskell for instance, you can create an endless list of random numbers on the impure side, and pass that list to your pure function. The implementation will generate the next number your pure function is using only when it needs it, but the function is still pure.

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