Search code examples
clojure

Clojure getting highest value from zipmap


So I've got my proposed zip map here and it works perfectly. As you can see I've got the data loading.

That is what it looks like in the repl which is perfect. And right here is the map

:Year 2020, :Day 27, :January 59, :February 38
:Year 2020, :Day 28, :January 41, :February 57
:Year 2020, :Day 29, :January 56, :February 51
:Year 2020, :Day 31, :January 94, :February -999
:Year 2020, :Day 30, :January 76, :February -999 

(map [:Day :Month

Bear in mind this is just a snippet of the code I've done. How would you propose I find the highest value day in January? And by highest I mean by the number next to the months

(into(sorted-map-by >(fn [:January]))Ha) 

I tried this to no success, The "Ha" at the end is just the name of the function where I am initialising the zipmap and using io/reader to read the file


Solution

  • I would use max-key and reduce:

    (def data [{:Year 2020, :Day 27, :January 59, :February 38}
               {:Year 2020, :Day 28, :January 41, :February 57}
               {:Year 2020, :Day 29, :January 56, :February 51}
               {:Year 2020, :Day 31, :January 94, :February -999}
               {:Year 2020, :Day 30, :January 76, :February -999}])
    
    (reduce (partial max-key :January) data)
    ;; => {:Year 2020, :Day 31, :January 94, :February -999}
    
    (:Day (reduce (partial max-key :January) data))
    ;; => 31