Clojure问号箭头宏

时间:2016-09-08 10:11:51

标签: clojure macros

我在?>块中遇到了->语句,其中包含一些我试图理解的代码。我搜查了clojuredocs,但是空白了。这是做什么的?

我试图理解的代码:

(-> (apply time/t -2 date-fields)
             (?> (:grain token-fields) (assoc :grain (:grain token-fields)))
             (?> (:timezone token-fields) (assoc :timezone (:timezone token-fields))))

1 个答案:

答案 0 :(得分:6)

我找到了。它来自Plumbing库。

引用the docs

?> macro
(?> arg do-it? & rest)
Conditional single-arrow operation (-> m (?> add-kv? (assoc :k :v)))

以下是宏的实际源代码:

(defmacro ?>
  "Conditional single-arrow operation (-> m (?> add-kv? (assoc :k :v)))"
  [arg do-it? & rest]
  `(if ~do-it?
     (-> ~arg ~@rest)
     ~arg))