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 ofsum
, which is just a regular Clojure function definition
精彩评论