轉載:http://www.iteye.com/news/117
Erlang是近兩年非常吸引眼球的函數式程式設計語言,因為Erlang能夠做到code-as-data,以及資料不變的特性,因此非常適合大規模,高並發負載的應用環境。特別是隨著現在多核CPU的廣泛應用,並行運算成為了一個熱點話題。
作為當今最主流的運算平台JVM,把函數式程式設計語言引入JVM也是很多人嘗試的方向,Clojure就是其中之一。Clojure是一個在JVM平台啟動並執行動態函數式程式設計語言,其文法解決於LISP語言,在JVM平台啟動並執行時候,會被編譯為JVM的位元組碼進行運算。
Clojure保持了函數式語言的主要特點,例如immutable state,Full Lisp-style macro support,persistent data structures等等,並且還能夠非常方便的調用Java類庫的API,和Java類庫進行良好的整合。
Java整合樣本:
Java代碼
- (new java.util.Date)
- => Wed Oct 17 20:01:38 CEST 2007
-
- (. (new java.util.Date) (getTime))
- => 1192644138751
-
- (.. System out (println "This is cool!"))
- This is cool!
(new java.util.Date)=> Wed Oct 17 20:01:38 CEST 2007(. (new java.util.Date) (getTime))=> 1192644138751 (.. System out (println "This is cool!"))This is cool!
Lisp風格的宏 Java代碼
- (defmacro time [form]
- `(let [t0# (. System (currentTimeMillis))
- res# ~form
- t1# (. System (currentTimeMillis))]
- (.. System out (println (strcat "Execution took "
- (/ (- t1# t0#) 1000.0) " s")))
- res#))
-
- Usage:
- (defn factorial [n]
- (if (< n 2)
- 1
- (* n (factorial (- n 1)))))
-
- (time (factorial 1000))
- => Execution took 0.012 s
- 40…
(defmacro time [form] `(let [t0# (. System (currentTimeMillis)) res# ~form t1# (. System (currentTimeMillis))] (.. System out (println (strcat "Execution took " (/ (- t1# t0#) 1000.0) " s"))) res#))Usage:(defn factorial [n] (if (< n 2) 1 (* n (factorial (- n 1)))))(time (factorial 1000))=> Execution took 0.012 s 40…
也許,Clojure將成為JVM平台的Erlang,想想看,Clojure還能夠直接調用Java的類庫,真是令人興奮。
Clojure的首頁:
http://clojure.sourceforge.net/