這篇筆記介紹不可變數據, Persistent Data Structure 和 Immutable.
可是不深刻數據結構實現, 函數式編程理論.javascript
https://en.wikipedia.org/wiki/Persistent_data_structurehtml
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.java
FP: Haskell, Clojure, Elixir
類庫: immutable-jsreact
http://vkostyukov.ru/posts/designing-a-pfds/git
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.github
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...api
Clojure 的數據結構實現
Understanding Clojure's Persistent Vectors, pt. 1
http://hypirion.com/musings/understanding-persistent-vector-pt-1
安全
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
A Functional Approach to Standard Binary Heaps
http://arxiv.org/pdf/1312.4666v1.pdf
有變量. 可是複合數據結構是不可變的
clojure(def x 1) (defn p [] x) (p) (def x 2) (p)
沒有變量的寫法, 只能用 let
定義表達式 alias name
或者在 do notation 裏有賦值, 但底層不是賦值
https://en.wikibooks.org/wiki/Haskell/do_notation
haskelldo x1 <- action1 x2 <- action2 action3 x1 x2
haskellaction1 >>= \ x1 -> action2 >>= \ x2 -> action3 x1 x2
變量被引用就是不可變的值(不過在 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
http://www.quora.com/Is-object-immutability-in-functional-programming-...
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
jsshouldComponentUpdate: function(nextProps, nextState) { return true; }
https://facebook.github.io/react/docs/advanced-performance.html
http://facebook.github.io/immutable-js/docs/#/
https://github.com/intelie/immutable-js-diff
https://github.com/intelie/immutable-js-patch
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.
haskellfoldl f z [] = z foldl f z (x:xs) = foldl f (f z x) xs
haskellmaximum' :: (Ord a) => [a] -> a maximum' [] = error "maximum of empty list" maximum' [x] = x maximum' (x:xs) = max x (maximum' xs)
http://www.haskellforall.com/2012/01/haskell-for-c-programmers-for-loo...
haskellwhile :: (Monad m) => m Bool -> m a -> m () while cond action = do c <- cond when c $ do action while cond action for :: (Monad m) => m a -> m Bool -> m b -> m c -> m () for init cond post action = do init while cond $ do action post
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