开发者

What does this clojure code do?

开发者 https://www.devze.com 2023-04-11 02:36 出处:网络
(ns utils (:gen-class :name Utils :methods [#^{:static true} [sum [java.util.Collection] long]])) (defn sum [coll] (reduce + coll))
(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

精彩评论

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

关注公众号