Skip to content

Instantly share code, notes, and snippets.

@BadUncleX
Last active March 31, 2018 11:07
Show Gist options
  • Save BadUncleX/3b46f6b80a59bdb56f9d511268fbd239 to your computer and use it in GitHub Desktop.
Save BadUncleX/3b46f6b80a59bdb56f9d511268fbd239 to your computer and use it in GitHub Desktop.
delay 用法
;;
;; 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))))
;;
;; 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)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment