纯功能数据结构的好处是什么? [英] What is the benefit of purely functional data structure?

查看:167
本文介绍了纯功能数据结构的好处是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据结构上有大量的文本,数据结构的代码库。我明白纯功能的数据结构更容易理解。然而,我无法理解在实际代码中使用纯功能数据结构(使用功能编程语言)而不是强制对手的真实世界优势。有人可以提供一些纯粹的功能数据结构具有优势的真实世界的案例,为什么?



像我在中使用 data_structure_name > programming_language 来执行应用程序,因为它可以执行 certain_thing



谢谢。



PS:我的意思是纯功能数据结构与持久数据结构不一样。持久的数据结构是不会改变的数据结构?另一方面,纯功能数据结构是纯粹运行的数据结构。

解决方案

纯功能(又称持久性或不可变)数据结构给你几个优点:




  • 你永远不必锁定它们,这极大地改善了并发

  • 他们可以共享结构,其中减少内存使用。例如,考虑Haskell中的列表[1,2,3,4]和一些诸如Java的命令式语言。要在Haskell中生成新的列表,您只需要创建新的 cons (一对值和引用到下一个元素),并将其连接到上一个列表。在Java中,您必须创建全新的列表,不会损坏以前的列表。

  • 您可以创建持久的数据结构 懒惰

  • 如果您使用功能性风格,您可以避免考虑时间和操作顺序等等,使您的程序更加声明式

  • 事实上,数据结构是不可变的,允许你做出更多的假设,所以扩展语言的能力。例如, Clojure 使用不变性的事实在每个对象上正确提供了hashCode()方法的实现,因此任何对象可能在地图中用作键。

  • 具有不可变数据和功能风格,您还可以自由使用 记忆



有更多的优点,一般来说,这是另一种建模真实世界的方法。 This and some other来自SICP的章节将为您提供更加准确的编程观点与不可变的结构,其优点和缺点。


There are large number of texts on data structures, and libraries of data structures code. I understand that purely functional data structure is easier to reason about. However I have trouble to understand the real world advantage of using purely functional data structure in pragmatic code (using functional programming language or not) over the imperative counterpart. Can somebody provide some real world cases where purely functional data structure has advantage and why?

Examples along the line like I use data_structure_name in programming_language to do application because it can do certain_thing.

Thanks.

PS: What I mean by purely functional data structure is not the same as persistent data structure. Persistent data structure is a data structure that doesn't change?? On other hand purely functional data structure is a data structure that operates purely.

解决方案

Purely functional (aka persistent or immutable) data structures give you several advantages:

  • you never have to lock them, which extremely improves concurrency.
  • they can share structure, which reduces memory usage. For example, consider list [1, 2, 3, 4] in Haskell and some imperative language like Java. To produce new list in Haskell you only have to create new cons (pair of value and reference-to-next-element) and connect it to the previous list. In Java you have to create completely new list not to damage the previous one.
  • you can make persistent data structures lazy.
  • also, if you use functional style, you can avoid thinking of time and sequence of operations, and so, make your programs more declarative.
  • fact, that the data structure is immutable, allows you to make some more assumptions and so expand capabilities of language. For example, Clojure uses the fact of immutability to correctly provide implementations of hashCode() method on each object, so any object may be used as a key in a map.
  • with immutable data and functional style you can also freely use memoization.

There's much more advantages, in general, it is another way of modeling the real world. This and some other chapters from SICP will give you more accurate view of programming with immutable structures, its advantages and disadvantages.

这篇关于纯功能数据结构的好处是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆