Clojure 的量子特性

(defprotocol X (cat [this other]))

定義一個協議,這個和Objective-C的協議很像。不同於Java的接口。
user=> X
{:on-interface user.X, :on user.X, :sigs {:cat {:doc nil, :arglists ([this other]), :name cat}}, :var #'user/X, :method-map {:cat :cat}, :method-builders {#'user/cat #<user$eval12$fn__13 user$eval12$fn__13@13cc0a7f>}}

user=> (extend-type String X (cat [this other] (.concat this other)))
這個就很ObjC的風格。把一個函數作爲一個類型在一個協議下的實現,定義出來。
user=> (cat "AA" "BB")
"AABB"
就思想上來說,FP和OO是反的。一個以函數爲核心,一個以數據爲核心。
而Lisp,則是分兩個層面,就基礎層,一切都屬數據。但是在抽象層,一切都是函數和函數調用了。
或者可以這麼說,Lisp有類似量子光學裏面的波粒二相性的特性,“動”——是 函數調用,“靜”——是 數據。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章