foxlog
3/30/2018 - 8:38 AM

delay 用法

delay 用法

;;
;; io.aviso.exception.clj
;; 
;;
(def ^:private current-dir-prefix
  "Convert the current directory (via property 'user.dir') into a prefix to be omitted from file names."
  ;; 获得用户目录, 确实只需要执行一次即可
  (delay (str (System/getProperty "user.dir") "/")))

;;
;; leingen.repl.clj
;;
(def ack-server
  "The server which handles ack replies."
  ;; 服务器启动, 只需一次
  (delay (nrepl.server/start-server
          :bind (repl-host nil)
          :handler (nrepl.ack/handle-ack nrepl.server/unknown-op))))

;;
;; korma.db.clj
;;
(defn delay-pool
  "Return a delay for creating a connection pool for the given spec."
  [spec]
  ;;连接池的初始化只需要执行一次
  (delay (connection-pool spec)))
;;
;; clojure.core
;; 延迟执行并缓存
;; add something
(comment
  (defmacro delay
   "Takes a body of expressions and yields a Delay object that will
   invoke the body only the first time it is forced (with force or deref/@), and
   will cache the result and return it on all subsequent force
   calls. See also - realized?"
   {:added "1.0"}
   [& body]
   (list 'new 'clojure.lang.Delay (list* `^{:once true} fn* [] body))))