`
linkerlin
  • 浏览: 36180 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

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有类似量子光学里面的波粒二相性的特性,“动”——是 函数调用,“静”——是 数据。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics