liveBook · Manning

Merge

As explained in Clojure: The Essential Reference

merge is a Clojure function that combines maps by copying key-value pairs and letting later values replace earlier values for conflicting keys.

merge combines one or more maps by copying key-value pairs into the first map. When keys conflict, later values replace earlier ones; the first map determines the output type. section 11.3.3

Key behavior and edge cases:

  • With no arguments or only nil arguments, returns nil; with one non-nil argument, returns that argument. section 11.3.3
  • Accepts Clojure map types such as hash maps, sorted maps, array maps, records, and structs; native java.util.HashMap is excluded. section 11.3.3
  • A sorted-map target requires incoming keys comparable with its existing keys; an array-map can promote to a hash-map as it grows. section 11.3.3

Resolve conflicts explicitly

(merge {:a 1 :b 2} {:b 9 :c 3}) ;; {:a 1, :b 9, :c 3} Use merge-with when conflicts need computation rather than replacement: (merge-with + {:a 1} {:a 10 :b 2}) ;; {:a 11, :b 2}

Listing 11.11. → Map merging, Map processing
(merge [& maps])

(merge-with [f & maps])

Browse the compact signatures for merge and merge-with. section 11.3.3

How Clojure: The Essential Reference covers it

How deep it goes
Defines inputs, outputs, conflicts, return types, exceptions, and sorted-map constraints, then demonstrates compositional patterns.
Examples
Demonstrates ordinary merging, numeric aggregation, collection accumulation, record addition, sorted-key failures, and a reusable merge helper.
What you can do after
Apply it via Clojure's merge function to combine maps and choose merge-with when conflicts require custom resolution.
How it connects
Connects merge with merge-with, maps, sorted maps, records, protocols, and persistent collections.

Also explained in

Compare how 5 books explain merge →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.