开发者

What does this clojure code do?

(ns utils
   (:gen-class :name Utils
               :methods [#^{:static true} [sum [java.util.Collection] long]]))

(defn sum [coll] (reduce + coll))

(defn -sum [coll] (sum coll))

Please explain 开发者_运维百科this code!


Having not used the Clojure gen-class facilities, my answer might be a little fuzzy:

This will generate the necessary byte-code which is about equivilant to the following Java pseudo code:

class Utils {
  public static long sum(Collection coll) {
    // Here goes the necessary code to call  (sum coll)
    // through the Clojure runtime
  }
}
  • -sum instructs Clojure to generate the Java method.
  • (sum coll) is the call to the first definition of sum, which is just a regular Clojure function definition
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜