Clojure def vs defn for a function with no arguments
I have written a program in clojure but some of the functions have no arguments. What would be the advantages of coding such functions as a "def" instead of a "defn" with no arguments?
def
s are evaluated only once whereas defn
s (with or without arguments) are evaluated (executed) every time they are called. So if your functions always return the same value, you can change them to def
s but not otherwise.
user=> (def t0 (System/currentTimeMillis))
user=> (defn t1 [] (System/currentTimeMillis))
user=> (t1)
1318408717941
user=> t0
1318408644243
user=> t0
1318408644243
user=> (t1)
1318408719361
(defn name ...)只是一个变成(def name(fn ...)的宏,不管它有多少个参数,所以它只是一个快捷方式,详见(doc defn)。
链接地址: http://www.djcxy.com/p/82566.html上一篇: ,公/私,默认