http://www.infoq.com/presentations/Value-Identity-State-Rich-Hickey
This presentation shows on what basis did Rich Hickey design the language Clojure which is similar to LISP but run on the JVM platform. It reveals one thing that is very interesting to our Java world:
How to implement immutable collections with which you can change at merely CONSTANT time.
Clojure implements immutable collections using ideal hash trees. First, we need to see how to locate an entry in an ideal hash tree.
Given a hash code (32-bit) for a map or index for a list, it's divided to 7 blocks, of which 6 blocks have 5-bit length and one block has 2-bit length. First we would search the first (right most) block which has 5 bits. The value of this block ranges from 0 to 31. It implies that each layer of this tree could have at most 32 nodes. So we directly place our attention to the calculated node. If the value of the first block is 7, we place our attention to the 8th node. If we find a leaf node, then that's it. It must be the one we are looking for. If not, we locate our attention on the second layer using the value of the second block. This way, we can find the desired entry in merely constant time, because the depth of the tree must be small. A tree with only 3 layers can contain 1,116,224 entries. It's large enough.
So how does it play the trick of changing this immutable tree in constant time?
Say we want to change the purple one to a new value. We create a new instance of the purple node with the new value object inside. Now, we know we can't change this purple one without changing his parent, the orange one. Also, we can't change the orange one without changing the green one. So, we end up with creating 3 new objects to substitute the green, orange and purple node. This way, we can change this tree in merely constant time!
With this new type of immutable collection implementation, we can substitute the traditional mutable ones in most cases except those have extremely strict performance restriction. What can we gain from using immutable collections in the field of enterprise application development? Due to the complexity of manipulating collections, we often want to define a domain object that carries some type of collection to an entity instead of a value object. Once the mutable entities loose control, the whole application is going to lose control. Google Collections 1.0 contains some immutable versions of the JDK collections but not enough. They just throw exceptions when you invoke the modification methods instead of returning a new, changed instance. That's because of the interface restrictions of the JDK Collections API. What about the brand new language, like Scala? It has the complete support for immutable collections. Unfortunately, the implementation is linear most of the time except for some special ones (like head and tail methods of the List class), which means every time you change the collection, it's copied entirely! This kind of performance cost is sometimes unacceptable to some criticial applications. Now here comes the savior. Although the implementation hasn't been migrated to Java, we can do this easily because Clojure is written in Java.
- 大小: 63.6 KB
- 大小: 65.4 KB
分享到:
相关推荐
Programming Clojure is an inspiration of Clojure knowledge and has furthered my understanding of the nuances of Clojure. One of the new sections includes a step-by-step on building an application that...
《inspiration 7.6绿化版:思维导图软件的高效使用指南》 在信息化时代,思维导图作为一种有效的信息组织和学习工具,受到了广大用户的喜爱。Inspiration 7.6绿化版是一款专为此目的设计的软件,它以其强大的功能和...
标题中的“Inspiration”可能指的是一个与设计、艺术或创意工作相关的主题,特别是考虑到标签是“字体”。在IT行业中,字体扮演着至关重要的角色,它不仅关乎文本的可读性,还涉及到用户界面的设计美学、品牌识别度...
inspiration DX3.0模板】是针对Discuz!论坛系统的一款定制化模板,专为提升用户体验和网站视觉效果而设计。Discuz!是一款广泛使用的社区论坛软件,它提供了一个平台,让用户可以轻松创建和管理自己的在线论坛。这款...
绝对安全 解压即用 无需注册是一款功能强大的以制作思维导图为主要功能的软件,inspiration软件,可以让使用者更清楚要点之间的联系,使思维活动更活跃,思路更清晰,较之文字段落更能促进学习及记忆,本站提供...
- **标题**:“INSPIRATION 动态版(2013).pptx”表明这是一份名为“灵感”的动态版本PPT模板,发布于2013年。 - **描述**:该模板被定义为“PPT模板”,说明它主要应用于演示文稿的制作。 - **标签**:“ppt”...
放在安装目录如S:\Inspiration 9\Libraries\Custom Libraries,结合默认的矩形和圆角矩形即可完成程序框图的绘制。
"Inspiration中文教程" Inspiration是一款功能强大的思维导图软件,能够帮助用户快速组织和处理各种信息。本教程将引导您快速掌握Inspiration的基本使用方法,并了解其强大功能。 一、在WINDOWS中启动Inspiration ...
【Inspiration在化学教学的应用】 化学教学中,有效的知识组织和呈现对于学生理解和掌握复杂的化学概念至关重要。传统教学方式依赖于教师的板书,但这种方式受限于空间和形式,难以应对化学学科的多层次和多样性。...
标题“inspiration”通常在IT领域可能指的是编程、设计或者创新思维的启发,但缺乏具体的标签来明确这个主题。不过,我们可以从一个宽泛的角度来探讨“inspiration”在IT行业中的不同含义。 1. 编程灵感:在编程...
Mobile Robots Inspiration to Implementation - Flynn and Jones, 2nd Edition
Dell 灵越 Inspiration 5420 黑苹果 Hackintosh 四叶草 Clover EFI, 可直接用于 MacOS High Sierra 的安装。 使用步骤: 使用命令写好U盘之后,将Clover安装到U盘上(选 自定义,然后只勾选前两个,即只安装EFI相关...
### 编程作为一种体验:自我启发 #### 一、引言与背景介绍 在IT行业中,编程不仅是技术实践的一部分,也是一种独特的创作体验。本文旨在深入探讨由R.B.Smith等作者于1994年在OOPSLA会议上发表的研究成果——《基于...
Drawing inspiration from the success of Seymour Papert’s LOGO project at MIT, Rich designed an introductory programming environment in which students teach a robot to solve simple problems.That ...
inspiration from vision of locusts in detecting the fast approaching objects. Research suggested that locusts use widefield visual neuron called the lobula giant movement detector to respond to ...
The Music Player draws inspiration from paullewis's music-dna. The Player uses the FFT Data supplied by the Visualizer class of Android , calculates the Amplitude at that particular moment and plots ...
Drawing inspiration from otherwise computationally constraining Computer Vision techniques, we present a system combining visual, inertial and depth information to achieve dense, local scene ...