1

我正在尝试制作core.reducers的头部或尾部,并且遇到以下代码行r/map

(defmacro ^:private defcurried
  "Builds another arity of the fn that returns a fn awaiting the last
  param"
  [name doc meta args & body]
  (do-curried name doc meta args body))

(defn- do-rfn [f1 k fkv] 
  `(fn
     ([] (~f1))
     ~(clojure.walk/postwalk
       #(if (sequential? %)
          ((if (vector? %) vec identity)
           (core/remove #{k} %))
          %)
       fkv)
     ~fkv))

(defmacro ^:private rfn
  "Builds 3-arity reducing fn given names of wrapped fn and key, and k/v impl."
  [[f1 k] fkv]
  (do-rfn f1 k fkv))

(defcurried map
  "Applies f to every value in the reduction of coll. Foldable."
  {:added "1.5"}
  [f coll]
  (folder coll
          (fn [f1]
            (rfn [f1 k]
                 ;; the following is k/v implementation but
                 ([ret k v] ;; why a `vector` is placed in the beginning of a list???
                  (f1 ret (f k v)))))))

defcurried map实施中,这rfn对我来说看起来很奇怪和不寻常,因为我不明白为什么[ret k v]放在列表的开头。谁能解释一下?

4

1 回答 1

1

当作为函数调用时,向量、关键字、符号或哈希映射最终都会get适当地调用。

user> (get [:a :b :c] 1)
:b
user> ([:a :b :c] 1)
:b
于 2014-09-07T05:49:16.750 回答