Is there anything for map which operates like update-in and assoc-in, where
we can call a function with the value looked up in a nested structure?
What I've come up with:
(defn map-in
"Returns a lazy sequence consisting of the results of
calling map on coll, for each value in the coll,
extracting the value using keys ks, finally applying f the
that value and args: (apply f item args)"
[coll [& ks] f & args]
(map (fn [item]
(let [value (reduce #(%2 %1) item ks)]
(apply f value args)))
coll))
Which could then be used like:
(def customers [{:name "Alice" :address {:city "Raleigh" :state "NC"}}
{:name "Bob" :address {:city "Seattle" :state "WA"}}])
(map-in customers [:address :state] #(-> % clojure.string/lower-case
keyword))
=> (:nc :wa)
Just wondering if I am re-inventing something here.
Thanks,
Steve
--
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.