解释clojure map reducer实现

时间:2014-09-07 03:52:10

标签: clojure reducers

我正试图制作core.reducers library的头部或尾部,并且我遇到了关于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]放在列表的开头。有人可以解释一下吗?

1 个答案:

答案 0 :(得分:1)

当作为函数调用时,矢量,关键字,符号或散列映射都将最终调用get

user> (get [:a :b :c] 1)
:b
user> ([:a :b :c] 1)
:b