Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using swap to MERGE (append to) a nested map in a Clojure atom?

Tags:

clojure

Let's say I have an atom that contains a map like this:

{:count 0 :map hash-map}

How can I use swap to merge another key-value pair onto :map?

like image 233
szxk Avatar asked Mar 10 '14 02:03

szxk


People also ask

How do I merge two maps in Java?

To merge Maps, use the spread operator (...) to unpack the values of two or more Maps into an array and pass them into the Map () constructor, e.g. new Map ( [...map1, ...map2]). The new Map will contain the key-value pairs from all provided Map objects.

What is a map in CL Clojure?

Clojure - Maps. A Map is a collection that maps keys to values. Two different map types are provided - hashed and sorted. HashMaps require keys that correctly support hashCode and equals. SortedMaps require keys that implement Comparable, or an instance of Comparator.

What is the use of merge method in HashMap?

The merge (Key, Value, BiFunctional) method of HashMap class is used to combine multiple mapped values for a key using the given mapping function. Bucket is actually an index of array, that array is called table in HashMap implementation. So table [0] is referred to as bucket0, table [1] as bucket1 and so on.


1 Answers

You'd use assoc-in:

(swap! my-atom assoc-in [:map :new-key] value)
like image 187
Max Noel Avatar answered Sep 22 '22 17:09

Max Noel