(ns utils
(:gen-class :name Utils
:methods [#^{:static true} [sum [java.util.Collection] long]]))
(defn sum [coll] (reduce + coll))
(defn -sum [coll] (sum coll))
Run Code Online (Sandbox Code Playgroud)
请解释一下这段代码!
没有使用Clojure的基础设施,我的回答可能有点模糊:
这将生成必要的字节代码,该代码与以下Java伪代码大致相同:
class Utils {
public static long sum(Collection coll) {
// Here goes the necessary code to call (sum coll)
// through the Clojure runtime
}
}
Run Code Online (Sandbox Code Playgroud)
-sum
指示Clojure生成Java方法.(sum coll)
是对第一个定义的调用sum
,它只是一个常规的Clojure函数定义