user=>This worked for all of the ways the data was accessed, until someone asked for a list of all people and all languages.
(def my-data {"Jay"
{"clojure"
{:name "Jay", :language "clojure", :enjoys true},
"ruby"
{:name "Jay", :language "ruby", :enjoys true}}
"Jon"
{"java"
{:name "Jon", :language "java", :enjoys true}}} )
#'user/my-data
user=> (get-in my-data ["Jon" "java"])
{:name "Jon", :language "java", :enjoys true}
user=> (get-in my-data ["Jay" "ruby"])
{:name "Jay", :language "ruby", :enjoys true}
I needed a function that grabbed all the values nested to a point in the map (grabbing only the values instead of the deepest map wouldn't have provided valuable information). I created the following function that should grab all the values up to the nesting level specified.
(defn nth-vals* [a i m]The nth-vals function can be used as the following example shows. (assuming the same map for my-data) (formatted)
(if (and (map? m) (> i 0))
(reduce into a (map (fn [v] (nth-vals* a (dec i) v)) (vals m)))
(conj a m)))
(defn nth-vals [i m]
(if (nil? m)
{}
(nth-vals* [] i m)))
user=> (nth-vals 2 my-data)For reference, here's what's returned if we reduce the map all the way to it's values.
[{:name "Jay", :language "clojure", :enjoys true}
{:name "Jay", :language "ruby", :enjoys true}
{:name "Jon", :language "java", :enjoys true}]
user=> (nth-vals 3 my-data)That list of values may be helpful for someone else, but it wouldn't have solved our current problem.
["Jay" "clojure" true "Jay" "ruby" true "Jon" "java" true]
And, if you're interested, here's what's returned if you ask for 1 level deep of values. (formatted, again)
user=> (nth-vals 1 my-data)I wouldn't at all be surprised if something like this already exists in Clojure core, but I haven't found it yet.
[{"clojure" {:name "Jay", :language "clojure", :enjoys true},
"ruby" {:name "Jay", :language "ruby", :enjoys true}}
{"java" {:name "Jon", :language "java", :enjoys true}}]
'for' can be useful for unpacking nested structures:
ReplyDelete;; same as (nth-vals 2 my-data)
(for [[_ vs] my-data [_ vvs] vs] vvs)
;; same as (nth-vals 3 my-data)
(for [[_ vs] my-data [_ vvs] vs [_ vvvs] vvs] vvvs)