The reduce in your mapped function is already implemented by get-in. Other 
possible implementations using get-in:

(defn map-in [coll ks f & args]
  (->> coll
       (map #(get-in % ks))
       (map #(apply f % args))))

Or:

(defn map-in [coll ks f & args]
  (map #(apply f (get-in % ks) args) coll))


On Tuesday, March 24, 2015 at 9:16:58 PM UTC-5, Steve Ashton wrote:
>
> 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 clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
  • map-in Steve Ashton
    • Re: map-in Francis Avila

Reply via email to