Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In clojure, how to reverse a map hierarchy [duplicate]

Tags:

clojure

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}}
like image 414
viebel Avatar asked May 14 '14 09:05

viebel


2 Answers

You could use a list comprehension and some destructuring, like

user=> (->> (for [[day consum] data 
  #_=>            [fruit amount] consum]
  #_=>         {fruit {day amount}})
  #_=>      (apply merge-with conj))
{:orange {:tuesday 2}, :banana {:tuesday 5, :monday 2}, :apple {:monday 3}}
user=>

or using a function + mapcat instead:

(defn flip [[day consum]]
    (map (fn [[fruit amount]] {fruit {day amount}}) consum))

(apply merge-with conj (mapcat flip data))
like image 82
sloth Avatar answered Oct 06 '22 00:10

sloth


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}})))
like image 26
Juan Manuel Avatar answered Oct 05 '22 23:10

Juan Manuel