Skip to content

Commit

Permalink
[new] Add merge-nx
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaoussanis committed Oct 30, 2024
1 parent e2cae57 commit dc06199
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/taoensso/encore.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2821,8 +2821,8 @@

(defn merge
"Like `core/merge` but:
- Often faster, with much better worst-case performance.
- Supports `:merge/dissoc` vals."
- Supports `:merge/dissoc` vals.
- Often faster, with much better worst-case performance."
([ ] nil)
([m1 ] (p! m1))
([m1 m2 ] (p! (merge-with* false nil m1 m2)))
Expand All @@ -2831,9 +2831,9 @@

(defn nested-merge
"Like `core/merge` but:
- Often faster, with much better worst-case performance.
- Recursively merges nested maps.
- Supports `:merge/dissoc` vals.
- Recursively merges nested maps."
- Often faster, with much better worst-case performance."
([ ] nil)
([m1 ] (p! m1))
([m1 m2 ] (p! (merge-with* true nil m1 m2)))
Expand All @@ -2842,8 +2842,8 @@

(defn merge-with
"Like `core/merge-with` but:
- Often faster, with much better worst-case performance.
- Supports `:merge/dissoc` vals."
- Supports `:merge/dissoc` vals.
- Often faster, with much better worst-case performance."
([f ] nil)
([f m1 ] (p! m1))
([f m1 m2 ] (p! (merge-with* false f m1 m2)))
Expand All @@ -2852,15 +2852,27 @@

(defn nested-merge-with
"Like `core/merge-with` but:
- Often faster, with much better worst-case performance.
- Recursively merges nested maps.
- Supports `:merge/dissoc` vals.
- Recursively merges nested maps."
- Often faster, with much better worst-case performance."
([f ] nil)
([f m1 ] (p! m1))
([f m1 m2 ] (p! (merge-with* true f m1 m2)))
([f m1 m2 m3 ] (p! (merge-with* true f (merge-with* true f m1 m2) m3)))
([f m1 m2 m3 & more] (p! (merge-with* true f (cons (merge-with* true f (merge-with* true f m1 m2) m3) more)))))

(let [mf (fn [x y] x)]
(defn merge-nx
"Like `core/merge` but:
- Preserves existing values (so useful for merging defaults).
- Supports `:merge/dissoc` vals.
- Often faster, with much better worst-case performance."
([ ] nil)
([m1 ] (p! m1))
([m1 m2 ] (p! (merge-with* false mf m1 m2)))
([m1 m2 m3 ] (p! (merge-with* false mf (merge-with* false mf m1 m2) m3)))
([m1 m2 m3 & more] (p! (merge-with* false mf (cons (merge-with* false mf (merge-with* false mf m1 m2) m3) more))))))

(comment
(qb 1e6 ; [182.63 122.16 167.18]
(core-merge {:a 1} {:a {:b 1}} {:a {:c 1}})
Expand Down
5 changes: 5 additions & 0 deletions test/taoensso/encore_tests.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,11 @@

(is (= (enc/merge {:a 1} {:b 2} {:a 3} {:c 4}) {:a 3, :b 2, :c 4}))])

(testing "nx (preserve existing)"
[(is (= (enc/merge-nx {:a 1} {:a 2 :b 2}) {:a 1, :b 2}))
(is (= (enc/merge-nx {} {:a 2 :b 2}) {:a 2, :b 2}))
(is (= (enc/merge-nx {} {:a 2} {:a 3} {:b 4} {:a 5} {:c 6}) {:a 2, :b 4, :c 6}))])

(testing "Transient support (currently undocumented)"
[(is (= (enc/merge (transient {})) {}))
(is (= (enc/merge nil (transient {})) nil))
Expand Down

0 comments on commit dc06199

Please sign in to comment.