Skip to content

clojure.core/merge

View this page on ClojureDocs

Type: function Added: Clojure 1.0 Examples: 9 Runnable: 8
([& maps])

Returns a map that consists of the rest of the maps conj-ed onto the first. If a key occurs in more than one map, the mapping from the latter (left-to-right) will be the mapping in the result.

Examples

by dpritchett on
by ryo on
by phreed on
by dijonkitchen on
by genmeblog on
by liuchong on
by Crowbrammer on
by rainspeaker on . May have evaluation errors.
by raszi on

Note by udkl

When to use assoc, conj, merge

assoc, conj & merge behave very differently for different data structures

If you are writing a function that can handle multiple kinds of collections, then your choice will make a big difference.

Treat merge as a maps-only function (its similar to conj for other collections).

My opinion:

  • assoc - use when you are 'changing' existing key/value pairs
  • conj - use when you are 'adding' new key/value pairs
  • merge - use when you are combining two or more maps

Ref : https://stackoverflow.com/a/3204346

Note by rainspeaker

Merge is more similar to concat than to conj IMO.

See also


Content from the matching ClojureDocs page, with authors credited on each contribution.