In clojure, how to reverse a map hierarchy

clojure

Solution

My solution first transposes the pieces of the nested maps and then merges them all.

The pieces are transposed from `{k1 {k2 v}}`to `{k2 {k1 v}}` and then merged by `apply merge-with conj`

(defn map-reverse-hierarchy [mm]
   (apply merge-with conj
     (for [[k1 m] mm [k2 v] m] {k2 {k1 v}})))

Problem

In `clojure`, I have a map that contains for each day, and each fruit, the number of fruits eaten. I would like to "reverse the hierarchy" of the map and to return the same data but with the fruits at the top of the hierarchy. I will explain by an example: ``` (map-reverse-hierarchy {:monday {:banana 2 :apple 3} :tuesday {:banana 5 :orange 2}}) ; => {:orange {:tuesday 2}, ; :banana {:tuesday 5, :monday 2}, ; :apple {:monday 3}} ```

Original source

Related problems