欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  资讯频道

Clojure,jvm平台上的又一门函数式语言

程序员文章站 2022-05-21 09:23:07
...
Clojure 是什么?

引自官方网站的介绍
引用
Features
    * Dynamic Development
    * Functional Programming
    * Lisp
    * Runtime Polymorphism
    * Concurrent Programming
    * Hosted on the JVM

它基于JVM,能调用Java的类库,支持并发,与Scala(http://www.scala-lang.org/)很相似。先前曾有人将Scala比作是Java平台上的Erlang语言。

官方例子:基于Lisp语法,使用了Java并发库的并发编程
(import '(java.util.concurrent Executors))
(defn test-stm [nitems nthreads niters]
  (let [refs  (map ref (replicate nitems 0))
        pool  (. Executors (newFixedThreadPool nthreads))
        tasks (map (fn [t]
                      (fn []
                        (dotimes n niters
                          (sync nil
                            (dolist r refs
                              (set r (+ @r t)))))))
                   (range nthreads))]
      (. pool (invokeAll tasks))
      (. pool (shutdown))
      (map deref! refs)))
(test-stm 10 10 10000)
(550000 550000 550000 550000 550000 550000 550000 550000 550000 550000)

可见这里的并发只是使用了Java的线程,并非像Scala那样基于Actor模型。

在论坛 http://groups.google.com/group/clojure/browse_thread/thread/2a2b24ffef5d1631 上面,有人提出了这样的对比:

引用
Erlang: The Movie  <=> Clojure: The Podcast
Single Assignment  <=> Immutable Data Structures
Mnesia             <=> STM
ErlangVM           <=> JVM
Hipe               <=> JIT
Pattern Matching   <=> Multimethods
Erlang Shell       <=> REPL
Hot Code Reload    <=> Dynamic Compilation
Behaviours         <=> Extensible Abstractions
Tail Recursion     <=> recur
fun                <=> fn
syntax from 1987   <=> syntax from 1958
EMP2               <=> CL style macros
Fiber/Actor        <=> Termite


有兴趣的朋友,可以到 http://clojure.sourceforge.net/ 下载
相关标签: JVM Scala Erlang