Clojure深度合并以忽略nil值

我正在尝试在Clojure中深度合并多个地图.我在网上找到了很多解决方案,其中大部分都是这样的:

(defn deep-merge
  [& xs]
 (if (every? map? xs)
   (apply merge-with deep-merge xs)
  (last xs)))

这个解决方案的问题是,如果其中一个映射是nil,它将删除所有先前的映射(因此如果最后一个映射为nil,则整个函数将返回nil).在常规合并函数中不是这种情况,它忽略了nil值.
是否存在忽略nil值的深度合并的任何其他简单实现?

我发现这个: https://github.com/circleci/frontend/blob/04701bd314731b6e2a75c40085d13471b696c939/src-cljs/frontend/utils.cljs.它确实应该如此.

(defn deep-merge* [& maps]
  (let [f (fn [old new]
             (if (and (map? old) (map? new))
                 (merge-with deep-merge* old new)
                 new))]
    (if (every? map? maps)
      (apply merge-with f maps)
     (last maps))))

(defn deep-merge [& maps]
  (let [maps (filter identity maps)]
    (assert (every? map? maps))
   (apply merge-with deep-merge* maps)))

谢谢CircleCi人!

相关文章
相关标签/搜索