什么是破坏性更新?

发布于 2024-11-27 23:41:29 字数 74 浏览 0 评论 0原文

我看到很多与函数式编程相关的主题都提到了破坏性更新。我知道它类似于突变,所以我理解更新部分。但破坏性的部分是什么?还是我只是想太多了?

I see a lot of functional programming related topics mention destructive updates. I understand that it is something similar to mutation, so I understand the update part. But what is the destructive part? Or am I just over-thinking it?

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

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

发布评论

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

评论(2

情绪失控 2024-12-04 23:41:29

你可能有点想太多了。可变性就是它的全部;唯一被“破坏”的是您变异的之前的值

假设您正在使用某种搜索树来存储值,并且想要插入一个新的。找到新值所在的位置后,您有两个选择:

  • 使用不可变树,您可以沿着从新值的位置到根的路径构造新节点。不在路径上的子树将在新树中重用,如果您仍然有对原始树根的引用,则可以使用两者,并在它们之间共享公共子树。如果您有大量略有不同的副本,那么这可以节省空间,并且无需额外的努力,当然您还拥有不可变数据结构的所有常见好处。

  • 对于可变树,您可以将新值附加到它所属的位置,仅此而已;没有其他需要改变的。如果您只有一份副本,这几乎总是更快,并且可以节省内存分配,但任何引用“旧”树的内容现在都引用新树。原件已毁坏;它已经永远消失了。如果您需要保留原始版本,则必须在更改之前创建整个事物的全新副本。

如果“破坏”似乎是描述简单就地更新的一种不必要的严酷方式,那么您可能没有像我那样花那么多时间调试代码来找出地球上某个值在您背后被更改的时间。

You're probably overthinking it a bit. Mutability is all there is to it; the only thing being "destroyed" is the previous value of whatever you mutated.

Say you're using some sort of search tree to store values, and you want to insert a new one. After finding the location where the new values goes, you have two options:

  • With an immutable tree, you construct new nodes along the path from the new value's location up to the root. Subtrees not along the path are reused in the new tree, and if you still have a reference to the original tree's root you can use both, with the common subtrees shared between them. This economizes on space with no extra effort if you have lots of slightly-different copies floating around, and of course you have all the usual benefits of immutable data structures.

  • With a mutable tree, you attach the new value where it belongs and that's that; nothing else has to be changed. This is almost always faster, and economizes on memory allocation if you only ever have one copy around, but anything that had a reference to the "old" tree now has a reference to the new one. The original has been destroyed; it's gone forever. If you need to keep the original around, you have to go to the expense of creating an entirely new copy of the whole thing before changing it.

If "destruction" seems an unnecessarily harsh way to describe a simple in-place update, then you've probably not spent as much time as I have debugging code in order to figure out where on Earth some value is being changed behind your back.

眼中杀气 2024-12-04 23:41:29

命令式编程语言允许重新定义变量,例如

x = 1
x = 2

,因此 x 首先具有值 1,然后,它具有值 2。第二个操作是破坏性更新,因为 x 丢失了其等于 1 的初始定义。

这是不是普通数学中定义的处理方式。一旦定义,变量就保持其值。
上面的方程被视为方程组,可以从第二个方程中减去第一个方程,这将给出

x - x = 2 - 1 <=> 0 = 1

这是一个错误的陈述。假设一旦引入,x 是相同的。

像这样熟悉的陈述

x = x + 1

会得出相同的结论。

函数式语言对变量的使用相同,一旦定义它们就无法重新分配它们。上面的语句将变成

x2 = x + 1

,我们将没有 forwhile 循环,而是递归或一些高阶函数。

The imperative programming languages allow variables to be redefined, e.g

x = 1
x = 2

So x first has the value 1 then, later, it has the value 2. The second operation is an destructive update, because x looses its initial definition as being equal to 1.

This is not how definition is handled in common mathematics. Once defined, a variable keeps its value.
The above, seen as system of equations, would allow to subtract the first from the second equation, which would give

x - x = 2 - 1 <=> 0 = 1

which is a false statement. It is assumed that once introduced, x is the same.

A familiar statement like

x = x + 1

would lead to the same conclusion.

The functional languages have the same use of variables, once they are defined it is not possible to reassign them. The above statement would turn into

x2 = x + 1

and we would have no for or while loop but rather recursion or some higher order function.

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