Sometimes, you'd like to merge multiple collections into a single collection or split a single collection into multiple collections. Let's have a look these cases.
You only need REPL, described in the Repl up! recipe in Chapter 1, Live Programming with Clojure, and no additional libraries. Start REPL, and you can review the sample code in this recipe.
Let's see how to merge and split collections.
The merge
function merges multiple maps into a single map. If there are the same keys among maps in arguments, latter entries override former entries:
(merge {:a 1 :b 2 :c 3} {:c 4 :d 5 :e 6}) ;;=> {:a 1, :b 2, :c 4, :d 5, :e 6} (merge {:a 1 :b 2 :c 3} {:c 4 :d 5 :e 6} {:c 7 :f 4 :g 6}) ;;=> {:a 1, :b 2, :c 7, :d 5, :e 6, :f 4, :g 6}
The merge-with
function is more amazing:
(def nicos-fruits {:apple 10 :melon 15 :orange 2 :pear 12} ) ;;=> #'collection...