Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Clojure 合并一系列地图_Clojure_Hashmap - Fatal编程技术网

Clojure 合并一系列地图

Clojure 合并一系列地图,clojure,hashmap,Clojure,Hashmap,我有两张地图,形式如下: (def h1 {:p1 {:x {:port :p1 :instr :x :qty 10}}}) (def h2 {:p1 {:y {:port :p1 :instr :y :qty 11}}}) 当我使用 (apply (partial merge-with merge) [h1 h2]) 我得到了正确的结果: -> {:p1 {:y {:port :p1, :qty 11, :instr :y}, :x {:port :p1, :qty 10, :ins

我有两张地图,形式如下:

(def h1 {:p1 {:x {:port :p1 :instr :x :qty 10}}})
(def h2 {:p1 {:y {:port :p1 :instr :y :qty 11}}})
当我使用

(apply (partial merge-with merge) [h1 h2])
我得到了正确的结果:

-> {:p1 {:y {:port :p1, :qty 11, :instr :y}, :x {:port :p1, :qty 10, :instr :x}}
但是,如果我尝试缩小一系列贴图:

(reduce (fn [r m] apply (partial merge-with merge) r m) {} [h1 h2])
因此,我只得到第一张地图:

-> {:p1 {:y {:port :p1, :qty 11, :instr :y}}}

我也期待着同样的结果。我做错了什么?

您忘记应用
apply
。在

(fn [r m] apply (partial merge-with merge) r m)
。。。
fn
表单中的隐式
do
执行一系列计算,返回上一次计算的结果。忽略副作用(并且没有副作用),该功能相当于

(fn [r m] m)
正如你所观察到的

reduce
将贴图序列分开,因此不需要使用
apply

(reduce (fn [r m] (merge-with merge r m)) {} [h1 h2])
; {:p1
;  {:y {:qty 11, :instr :y, :port :p1},
;   :x {:qty 10, :instr :x, :port :p1}}}
如果您决定对函数使用相同的结构,则必须这样做:

(reduce (fn [r m] (apply (partial merge-with merge) [r m])) {} [h1 h2])
; {:p1
;  {:y {:qty 11, :instr :y, :port :p1},
;   :x {:qty 10, :instr :x, :port :p1}}}

apply
需要一个序列作为它的最后一个参数,它将该序列展平为它的第一个(函数)参数的尾随参数

您忘记应用
apply
。在

(fn [r m] apply (partial merge-with merge) r m)
。。。
fn
表单中的隐式
do
执行一系列计算,返回上一次计算的结果。忽略副作用(并且没有副作用),该功能相当于

(fn [r m] m)
正如你所观察到的

reduce
将贴图序列分开,因此不需要使用
apply

(reduce (fn [r m] (merge-with merge r m)) {} [h1 h2])
; {:p1
;  {:y {:qty 11, :instr :y, :port :p1},
;   :x {:qty 10, :instr :x, :port :p1}}}
如果您决定对函数使用相同的结构,则必须这样做:

(reduce (fn [r m] (apply (partial merge-with merge) [r m])) {} [h1 h2])
; {:p1
;  {:y {:qty 11, :instr :y, :port :p1},
;   :x {:qty 10, :instr :x, :port :p1}}}

apply
需要一个序列作为它的最后一个参数,它将该序列展平为它的第一个(函数)参数的尾随参数

谢谢。这突然变得很有道理。谢谢。这突然变得很有道理。