Pulling values from complex lists in Clojure
clojure
Solution
Fix the data structure and everything will fall in place. As of now your data structure isn't consistent at all and that will make any function that touch this data way more complicated and error prone. You can model this data as a map:
(def data {:a "whatever"
:b nil
:c "foo"
:e nil
:f "boo"
:g nil
:h nil
:i 62281})
And then to get the desired result:
(->> (vals data)
(filter (comp not nil?))
(into []))
But for some strange reason you still want to parse the data structure you provided then:
(defn get-values [data]
(->> (map #(if (map? %) (into [] %) %) data)
flatten
(filter #(or (string? %) (number? %)))
(into [])))
Problem
I'm trying to pull the values out of a complicated list structure. Given something like this: ``` [{:a "whatever" :b [:c "foo"]} :e {:f "boo"} :g {:h [:i 62281]}] ``` I'd like to get: ``` ["whatever" "foo" "boo" 62281] ``` So far I've only gotten to this point: ``` ((62281) nil (boo) nil whatever foo) ``` Here's the code: ``` (defn get-values [params] (apply conj (map (fn [part] (if (not (keyword? part)) (map (fn [v] (if (vector? v) (last v) v)) (vals part)))) params))) ``` - I can't seem to get rid of the nil's - I can't figure out why the values after a certain point are in lists. - I figure there's got to be a better way to do this.