1

输入:

List of keys: [ :name :address :work]
Map 1: { :name "A" :address "A Street" }
Map 2: { :work "Work Ave" }

输出:

([:name "A" nil] [:address "A Street" nil] [:work nil "Work Ave"])

这就是我目前所拥有的:

(defn maps-iterate [v & ms]
    (map (fn [k] (into [] [k #(map (k %) ms)])) v))

(println (maps-iterate [ :name :address :work ] { :name "A" :address "A Street"} { :work "Work Ave" }))

这给了我:

([:name #<user$maps_iterate$fn__2$fn__3 user$maps_iterate$fn__2$fn__3@4b14b82b>] 
 [:address #<user$maps_iterate$fn__2$fn__3 user$maps_iterate$fn__2$fn__3@3d47358f>] 
 [:work #<user$maps_iterate$fn__2$fn__3 user$maps_iterate$fn__2$fn__3@e0d5eb7>])
4

4 回答 4

2

尝试

(defn maps-iterate [v & ms]
    (map (fn [k] (into [] [k (map #(k %) ms)])) v))

甚至更好:

(defn maps-iterate [v & ms]
    (map (fn [k] (cons k (map k ms))) v))

注意:如果所有键都是关键字,那么您可以将它们用作函数:(map k ms)而不是(map #(k %) ms). 如果它们不是,那么您不能将它们用作函数。你需要写(map #(% k) ms)

于 2012-06-22T07:21:06.573 回答
1

这个怎么样?

(for [k ks]
  [k (map k [m1 m2])])
;;=> ([:name ("A" nil)] [:address ("A Street" nil)] [:work (nil "Work Ave")])

或者,如果您真的想要结果中的平面向量:

(for [k ks]
  (apply vector k 
         (map k [m1 m2])))
;;=> ([:name "A" nil] [:address "A Street" nil] [:work nil "Work Ave"])
于 2012-06-22T07:56:45.807 回答
1
user=> (def a { :name "A" :address "A Street" })
#'user/a
user=> (def b { :work "Work Ave" })
#'user/b
user=> (def c [ :name :address :work])
#'user/c
user=> (map #(vector %1 (%1 a) (%1 b)) c)
([:name "A" nil] [:address "A Street" nil] [:work nil "Work Ave"])
于 2012-06-22T08:25:37.743 回答
-1

以下 REPL 示例生成的输出不包含 nil 值:

user> (def map1 { :name "A" :address "A Street" })
#'user/map1
user> (def map2 { :work "Work Ave" })
#'user/map2
user> (seq (merge map1 map2))
([:work "Work Ave"] [:name "A"] [:address "A Street"])
于 2012-06-22T07:23:20.020 回答