There are only two steps: filter to select maps to use (e.g. :name =
"abc"), map to transform them (e.g. extract :value). Here are some examples:
(def xs [{:src "a.png", :name "abc", :value "a"} {:name "def", :src
"b.gif", :value "b"}])
=> #'user/xs
(->> xs
(filter (comp #{"abc" "def"} :name))
(map :value))
=> ("a" "b")
(->> xs
(filter (comp #{"abc"} :name))
(map :value))
=> ("a")
(->> xs
(filter #(= (:name %) "abc"))
(map #(select-keys % [:name :value])))
=> ({:value "a", :name "abc"})
(->> xs
(filter #(= (:name %) "abc"))
(map #(do {:name (:name %) :value (:value %)})))
=> ({:name "abc", :value "a"})
(as-> xs <>
(filter #(= (:name %) "abc") <>)
(clojure.set/project <> [:name :value]))
=> #{{:value "a", :name "abc"}}
On Friday, October 16, 2015 at 11:20:30 PM UTC-5, Mike wrote:
>
> I'm still new at this, thanks for any help.
>
> The following line extracts a value from a data structure:
>
> (:value (first (filter #(= (:name %) "abc") input-attrs)))
>
> *input-attrs* is a seq of hashes that for example looks like:
>
> ({:src "a.png", :name "abc", :value "a"} {:name "def", :src "b.gif", :value
> "b"})
>
> This is test data; the real data will have this structure but have more
> hashes. My goal is to return the :value value when a specific :name value
> is specified. The first line above is how to extract the :value value when
> :name = "abc". The problem is that I need to extract 2 :value values for 2
> distinct :name values from this one structure. The structure is moderately
> expensive to construct, so while I have it built I'd like to pull
> everything out of it that I need at once.
>
> It struck me that I should pass a vector of :name values to this function
> and have some sort of *for *or *map *process the vector over this
> extraction. But I cannot seem to write that correctly. It also struck me
> that once I can do the extractions correctly that I should probably pass a
> list of hashes back that has the result, such as:
>
> ({name "abc", :value "a"} {:name "def" :value "b"})
>
> But then that looks curiously very similar to the original structure
> (minus unmatched hashes and some unneeded other values). Am I just
> thinking about this "wrongly"? Thanks for any help.
>
>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.