这篇笔记介绍不可变数据, Persistent Data Structure 和 Immutable.
但是不深入数据结构实现, 函数式编程理论.


定义

https://en.wikipedia.org/wiki/Persistent_data_structure

In computing, a persistent data structure is a data structure that always preserves the previous version of itself when it is modified. Such data structures are effectively immutable, as their operations do not (visibly) update the structure in-place, but instead always yield a new updated structure.


编程语言

FP: Haskell, Clojure, Elixir
类库: immutable-js


概念的区别

http://vkostyukov.ru/posts/designing-a-pfds/

Immutability and persistent are quite similar terms, which often substitute each other. We say immutable vector (in Scala) but mean persistent vector (in Clojure): both implementations are based on the same abstract data structure Bit-Mapped Vector Trie but named differently. Although, there is a slight difference between immutability and persistence as they apply to data structures.

  • Persistent data structures support multiple versions
  • Immutable data structures aren't changeable

优势

http://stackoverflow.com/a/4400389/883571
http://www.quora.com/What-are-the-advantages-and-disadvantages-of-immu...

  • 多线程安全, 可靠
  • 对于需要多份数据的情况, 可以重用结构
  • 函数式编程

劣势

http://concurrencyfreaks.blogspot.sg/2013/10/immutable-data-structures...

  • 使用门槛
  • 需要编程语言实现好的 GC

底层实现

  • Clojure

Clojure 的数据结构实现
Understanding Clojure's Persistent Vectors, pt. 1
http://hypirion.com/musings/understanding-persistent-vector-pt-1

  • Haskell

Haskell lists are represented as singly linked list

haskelldata [] a = [] | a : [a]

http://stackoverflow.com/a/15063181/883571
http://i.stack.imgur.com/Y1Ajv.png

  • Paper

A Functional Approach to Standard Binary Heaps
http://arxiv.org/pdf/1312.4666v1.pdf


写法

  • Clojure

有变量. 但是复合数据结构是不可变的

clojure(def x 1)
(defn p []x)(p)
(def x 2)
(p)
  • Haskell

没有变量的写法, 只能用 let 定义表达式 alias name
或者在 do notation 里有赋值, 但底层不是赋值
https://en.wikibooks.org/wiki/Haskell/do_notation

haskelldo x1 <- action1x2 <- action2action3 x1 x2
haskellaction1 >>= \ x1 ->action2 >>= \ x2 ->action3 x1 x2
  • Elixir

变量被引用就是不可变的值(不过在 process 级别有私有数据)

iex(6)> a = 1
1
iex(7)> b = fn -> IO.puts a end
#Function<20.90072148/0 in :erl_eval.expr/5>
iex(8)> b.()
1
:ok
iex(9)> a = 2
2
iex(10)> b.()
1
:ok

性能问题

  • Performance

http://www.quora.com/Is-object-immutability-in-functional-programming-...

  • GHC

Data immutability forces us to produce a lot of temporary data but it also helps to collect this garbage rapidly.

https://wiki.haskell.org/GHC/Memory_Management

  • React 的性能优化
jsshouldComponentUpdate: function(nextProps, nextState) {return true;
}

https://facebook.github.io/react/docs/advanced-performance.html


immutable-js

http://facebook.github.io/immutable-js/docs/#/
https://github.com/intelie/immutable-js-diff
https://github.com/intelie/immutable-js-patch


seamless-immutable

This level of backwards compatibility requires ECMAScript 5 features like Object.defineProperty and Object.freeze to exist and work correctly, which limits the browsers that can use this library to the ones shown in the test results below. (tl;dr IE9+)

  • object.freeze

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glob...

jsvar o = Object.freeze(obj);

然而没有复用数据结构, 将会有性能问题:
https://medium.com/google-developers/javascript-application-architectu...

Well, you technically could use Object.freeze() to achieve immutability, however, the moment you need to modify those immutable objects you will need to perform a deep copy of the entire object, mutate the copy and then freeze it. This is often too slow to be of practical use in most use-cases.


怎样编写程序

  • sum
haskellfoldl f z []     = z
foldl f z (x:xs) = foldl f (f z x) xs
  • maximum
haskellmaximum' :: (Ord a) => [a] -> a
maximum' [] = error "maximum of empty list"
maximum' [x] = x
maximum' (x:xs) = max x (maximum' xs)
  • white/for

http://www.haskellforall.com/2012/01/haskell-for-c-programmers-for-loo...

haskellwhile :: (Monad m) => m Bool -> m a -> m ()
while cond action = doc <- condwhen c $ doactionwhile cond actionfor :: (Monad m) => m a -> m Bool -> m b -> m c -> m ()
for init cond post action = doinitwhile cond $ doactionpost
  • model
elm-- manage the model of our application over time
model : Signal Model
model =Signal.foldp update initialModel actions.signal

https://github.com/evancz/elm-todomvc/blob/master/Todo.elm#L314

一些链接, 关于不可变数据相关推荐

  1. python四种可变类型_SICP Python 描述 2.4 可变数据

    2.4 可变数据 我们已经看到了抽象在帮助我们应对大型系统的复杂性时如何至关重要.有效的程序整合也需要一些组织原则,指导我们构思程序的概要设计.特别地,我们需要一些策略来帮助我们构建大型系统,使之模块 ...

  2. Adobe Illustrator: Variable Data Adobe Illustrator 教程:可变数据 Lynda课程中文字幕

    Adobe Illustrator: Variable Data 中文字幕 Adobe Illustrator 教程:可变数据 中文字幕Adobe Illustrator: Variable Data ...

  3. pdf一键转曲_新技能:如何利用PDF制作可变数据!

    一提到制作可变数据,是不是就很头大?要在Excel里事先做好数据文本,设置多种功能,再利用Indesign.CorelDRAW排版制作? 今天,小编就给大家介绍一种简单方便的操作方法,仅利用PDF就能 ...

  4. Sql Server使用链接服务器远程取数据!

    由于最近开发的一个查询系统,基本是在其它服务器的,所以在本地服务器设计了中间,用来存从远程服务器取数据!     一种方法是通过,OPENDATASOURCE来远程读取数据! SELECT   * F ...

  5. java生成流水号001_可变数据如何批量生成?

    原标题:可变数据如何批量生成? 常见的可变数据批量生成有两种:"流水号数据的批量生成"和"连接数据库文件的数据生成".流水号数据就是类似001-002-003- ...

  6. 函数式编程——做到并发,不可变数据修改就只能复制后修改返回

    函数式编程 from:https://coolshell.cn/articles/10822.html 当我们说起函数式编程来说,我们会看到如下函数式编程的长相: 函数式编程的三大特性: immuta ...

  7. 从JS对象开始,谈一谈“不可变数据”和函数式编程

    文章转载自:https://segmentfault.com/a/1190000008780076 作为前端开发者,你会感受到JS中对象(Object)这个概念的强大.我们说"JS中一切皆对 ...

  8. sqlserver 微信 读取_Sql Server使用链接服务器远程取数据_sqlserver

    由于最近开发的一个查询系统,基本是在其它服务器的,所以在本地服务器设计了中间,用来存从远程服务器取数据! 一种方法是通过,OPENDATASOURCE来远程读取数据! SELECT   * FROM  ...

  9. 票据打印, 账单打印, 标签打印, 文档打印, 条码打印, 批量打印, 包装纸打印与设计,可变数据打印打印,发布,VC++源代码组件库解决方案...

    票据打印, 账单打印, 标签打印, 文档打印, 条码打印, 批量打印, 包装纸打印与设计,可变数据打印打印,发布,VC++源代码组件库解决方案 使用E-Form++源码库开发任何标签,票据,文档,条码 ...

最新文章

  1. php 记录用户行为路径,用户行为路径分析方法
  2. SQL 模糊查询技术
  3. linux下拒绝用户登录,Linux系统用户管理之禁止用户登录
  4. python 类和对象 atm_Python 类和对象
  5. Feature Pyramid Networks for Object Detection 论文笔记
  6. 性能相差极大的SQL语句
  7. vue2.0+stylus实现星级评定组件,computed计算属性实现全星半星,动态改变星级,多种星星规格
  8. C# MVC IOC、依赖注入
  9. linux系统中scp命令的用法
  10. 移动端 GPU 推理性能提升 2 倍!TensorFlow 推出新 OpenCL 后端
  11. C++单元测试工具CppUnit使用简介 【转载】
  12. c语言 由函数组成的数组,学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中,请编写函数fun(),它的_考题宝...
  13. 实验四+067+冯艳芳
  14. Tomcat内存溢出,解决方法
  15. 数据库查看内存,数据大小
  16. WM_SIZING 使用说明
  17. c语言找adc最小值,STC12C5A60S2 AD转换C语言示例程序(ADC查询方式)
  18. Python调用华为API实现人脸检测
  19. python图片转文字
  20. C++的异常(转载 )

热门文章

  1. 学习 Linux,101: 引导系统
  2. Fiddler抓包工具总结(转)
  3. 国内阿里Maven仓库镜像Maven配置文件Maven仓库速度快
  4. awk数组命令经典生产实战应用拓展
  5. 数据仓库的未来 MariaDB ColumnStore
  6. 软考之CPU的寻址方式
  7. log4j2配置实例[按小时记录日志文件]
  8. 如让自己想学不好shell编程都困难?
  9. 四百元值不值——论小米2A与2S
  10. 一个总裁做企业的十条心得