Re: Apply a function to some values in a map

2009-06-18 Thread Rowdy Rednose
You are absolutely right about the names. Unfortunately all the good ones are already taken by Rich. ;) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@goog

Re: Apply a function to some values in a map

2009-06-18 Thread Sean Devlin
Yeah, it's a bit ugly. It's designed to suppress this: user=> (list-to-map 1 2 3 4 5) {1 2, 3 4} user=> (hash-map 1 2 3 4 5) # I need to clean this up. Thanks for looking On Jun 18, 11:58 am, Rowdy Rednose wrote: > Interesting. It's a bit different from what I currently need, but I'm > curr

Re: Apply a function to some values in a map

2009-06-18 Thread Rowdy Rednose
Interesting. It's a bit different from what I currently need, but I'm currently looking at the sources. I wondered why you define this: (defn list-to-map [& params] (apply hash-map (reduce concat (partition 2 params Couldn't you just use the plain hash-map function? user=> (= (list-t

Re: Apply a function to some values in a map

2009-06-18 Thread Sean Devlin
Here's my solution to the problem. It's a bit long winded, so bear with me (or ignore it :)) I defined a function trans (defn trans [& params]...) Let me show an example: user=> (def test-map {:a 0 :b "B" :c "C"}) #'user/test-map user=> ((trans :count count) test-map) {:count 3, :a 0, :b "B"

Re: Apply a function to some values in a map

2009-06-18 Thread Chouser
On Thu, Jun 18, 2009 at 1:21 AM, Rowdy Rednose wrote: > > I've come up with this: > > (defn inc-values-in-map >  [map keys] >  (merge-with + map (zipmap keys (repeat 1 I'm glad you found a solution you liked better, but I like how succinct this one is. However, I would offer a friendly recom

Re: Apply a function to some values in a map

2009-06-18 Thread Rowdy Rednose
It's actually good to have default values for non-existing entries, as in Timothy's examples. Otherwise the previous version will throw NPEs. So: (defn map-map ([f map] (map-map f map (keys map) nil)) ([f map keys] (map-map f map keys nil)) ([f map keys default] (reduce (fn [m k] (asso

Re: Apply a function to some values in a map

2009-06-18 Thread Rowdy Rednose
Thanks for the ideas guys I especially like the reduce/assoc approach. It's the one most clear to me so far and also the most efficient one, I guess, as it does only one assoc per changed key, without creating other intermediate maps. So this is what I ended up using, with the nice side-effect t

Re: Apply a function to some values in a map

2009-06-18 Thread Stephen C. Gilardi
On Jun 18, 2009, at 2:39 AM, Stephen C. Gilardi wrote: (defn map-vals ([f m] (reduce conj {} (map (fn [[k v]] [k (f v)]) m))) ([f m keyseq] (conj m (map-vals f (select-keys m keyseq) I like the two-argument map-vals better in this version: (defn map-vals ([f m]

Re: Apply a function to some values in a map

2009-06-17 Thread Stephen C. Gilardi
On Jun 18, 2009, at 1:21 AM, Rowdy Rednose wrote: I've come up with this: (defn inc-values-in-map [map keys] (merge-with + map (zipmap keys (repeat 1 Aren't there more elegant ways to implement this function? Here's a shot at generalizing mapping over values in a map: (ns rednose) (

Re: Apply a function to some values in a map

2009-06-17 Thread Timothy Pratley
Your version is already succinct, is there something you particularly don't like about it? I would probabbly write it like this: (defn inc-values-in-map [map keys] (reduce #(assoc %1 %2 (inc (%1 %2 0))) map keys)) Because it caters better with (inc-values-in-map m [:one :three :one :four]) O