Re: Mapping a function over a map's values

2009-03-24 Thread Jon
Awesome. Thanks Konrad. On Mar 23, 4:11 am, Konrad Hinsen wrote: > On 22.03.2009, at 21:10, Jon Nadal wrote: > > > > > I often need to map a function over the values of a map while > > preserving keys--something like: > ... > > Is there a more concise way to do this in Clojure?  If not, is this

Re: Mapping a function over a map's values

2009-03-23 Thread Jeff Valk
On Mon, 23 Mar 2009 at 20:10, David Sletten wrote: > I think Mark was referring to the call to 'keys'. But apparently > Clojure doesn't need to traverse the map to generate the keys? The call to keys just creates a seq. There's no traversal until you consume it. > Not sure what you mean here.

Re: Mapping a function over a map's values

2009-03-23 Thread David Sletten
On Mar 23, 2009, at 5:24 AM, Jeff Valk wrote: > > On Mon, 23 Mar 2009 at 03:29, Mark Engelberg wrote: > >> But it traverses m twice, which is likely to be less efficient. > > I wondered about this too, and actually no. I think Mark was referring to the call to 'keys'. But apparently Clojure d

Re: Mapping a function over a map's values

2009-03-23 Thread Jeff Valk
On Mon, 23 Mar 2009 at 10:48, Konrad Hinsen wrote: > It is already in clojure.contrib, with exactly that implementation, > and for exactly that reason: Well I'm glad we agree then. :-) And thanks for the pointer. -Jeff --~--~-~--~~~---~--~~ You received this m

Re: Mapping a function over a map's values

2009-03-23 Thread Konrad Hinsen
On Mar 23, 2009, at 16:36, Jeff Valk wrote: >> I prefer the into version which allows to write a mapmap that >> preserves the >> map type (eg sorted or hash): >> >> (defn mapmap [f m] >> (into (empty m) (for [[k v] m] [k (f v)]))) > > Agreed. If it were in contrib, this would make most sense.

Re: Mapping a function over a map's values

2009-03-23 Thread Jeff Valk
On Mon, 23 Mar 2009 at 07:57, Christophe Grand wrote: > I prefer the into version which allows to write a mapmap that preserves the > map type (eg sorted or hash): > > (defn mapmap [f m] > (into (empty m) (for [[k v] m] [k (f v)]))) Agreed. If it were in contrib, this would make most sense.

Re: Mapping a function over a map's values

2009-03-23 Thread Jeff Valk
On Mon, 23 Mar 2009 at 03:29, Mark Engelberg wrote: > But it traverses m twice, which is likely to be less efficient. I wondered about this too, and actually no. Zipmap is efficient. It constructs its return map in a single loop from two lazy seqs. Performance is practically identical to the "

Re: Mapping a function over a map's values

2009-03-23 Thread Konrad Hinsen
On Mar 23, 2009, at 14:26, Mark Volkmann wrote: >> That leaves the question whether there is still any need for the >> clojure.lang.ISeq implementation. At the moment it is used for lists, >> but that is not particularly consistent. I guess I will remove it. > > Maybe you should keep it to suppor

Re: Mapping a function over a map's values

2009-03-23 Thread Mark Volkmann
On Mon, Mar 23, 2009 at 7:04 AM, Konrad Hinsen wrote: > > On Mar 23, 2009, at 12:15, Mark Volkmann wrote: > >>> Look at clojure.contrib.generic.functor/fmap. It does what you need >>> for maps, for all the other Clojure collections (where it works like >>> map except that its return value is a co

Re: Mapping a function over a map's values

2009-03-23 Thread Christophe Grand
I prefer the into version which allows to write a mapmap that preserves the map type (eg sorted or hash): (defn mapmap [f m] (into (empty m) (for [[k v] m] [k (f v)]))) Christophe On Mon, Mar 23, 2009 at 7:53 AM, Jeff Valk wrote: > > On Sun, 22 Mar 2009 at 23:27, Jeff Valk wrote: > > > Ah, go

Re: Mapping a function over a map's values

2009-03-23 Thread Konrad Hinsen
On Mar 23, 2009, at 12:15, Mark Volkmann wrote: >> Look at clojure.contrib.generic.functor/fmap. It does what you need >> for maps, for all the other Clojure collections (where it works like >> map except that its return value is a collection of the same type as >> the input value), and you can i

Re: Mapping a function over a map's values

2009-03-23 Thread Mark Volkmann
On Mon, Mar 23, 2009 at 3:11 AM, Konrad Hinsen wrote: > > On 22.03.2009, at 21:10, Jon Nadal wrote: > >> I often need to map a function over the values of a map while >> preserving keys--something like: > ... >> Is there a more concise way to do this in Clojure?  If not, is this >> something that

Re: Mapping a function over a map's values

2009-03-23 Thread Mark Engelberg
On Sun, Mar 22, 2009 at 11:53 PM, Jeff Valk wrote: > For the record, I think the original approach is the most clear. And it's > actually shorter. > > (defn mapmap [f m] >  (zipmap (keys m) (map f (vals m But it traverses m twice, which is likely to be less efficient. --~--~-~--~--

Re: Mapping a function over a map's values

2009-03-23 Thread Konrad Hinsen
On 22.03.2009, at 21:10, Jon Nadal wrote: > I often need to map a function over the values of a map while > preserving keys--something like: ... > Is there a more concise way to do this in Clojure? If not, is this > something that might be worth putting in the contrib? Look at clojure.contrib.g

Re: Mapping a function over a map's values

2009-03-22 Thread Jeff Valk
On Sun, 22 Mar 2009 at 23:27, Jeff Valk wrote: > Ah, golf... :-) > > (defn mapmap [f m] > (into {} (for [[k v] m] [k (f v)]))) For the record, I think the original approach is the most clear. And it's actually shorter. (defn mapmap [f m] (zipmap (keys m) (map f (vals m --~--~---

Re: Mapping a function over a map's values

2009-03-22 Thread Kevin Downey
ooh for On Sun, Mar 22, 2009 at 9:27 PM, Jeff Valk wrote: > > On 22 March 2009 23:14, Timothy Pratley wrote: > >> Golf time! >> >> (defn mapmap [f m] >>     (into {} (map (fn [[x y]] [x (f y)]) m))) > > Ah, golf... :-) > > (defn mapmap [f m] >  (into {} (for [[k v] m] [k (f v)]))) > > > > --

Re: Mapping a function over a map's values

2009-03-22 Thread Jeff Valk
On 22 March 2009 23:14, Timothy Pratley wrote: > Golf time! > > (defn mapmap [f m] > (into {} (map (fn [[x y]] [x (f y)]) m))) Ah, golf... :-) (defn mapmap [f m] (into {} (for [[k v] m] [k (f v)]))) --~--~-~--~~~---~--~~ You received this message becaus

Re: Mapping a function over a map's values

2009-03-22 Thread Timothy Pratley
Golf time! (defn mapmap [f m] (into {} (map (fn [[x y]] [x (f y)]) m))) On Mar 23, 2:51 pm, Kevin Downey wrote: > (defn mapmap [fn m] >     (into {} (map #(vector (first %) (fn (second %))) m))) --~--~-~--~~~---~--~~ You received this message because you ar

Re: Mapping a function over a map's values

2009-03-22 Thread Kevin Downey
(defn mapmap [fn m] (into {} (map #(vector (first %) (fn (second %))) m))) On Sun, Mar 22, 2009 at 1:10 PM, Jon Nadal wrote: > > I often need to map a function over the values of a map while > preserving keys--something like: > > [code] > (defn mapmap [fn m] >  (let [k (keys m) >        v (m

Mapping a function over a map's values

2009-03-22 Thread Jon Nadal
I often need to map a function over the values of a map while preserving keys--something like: [code] (defn mapmap [fn m] (let [k (keys m) v (map fn (vals m))] (zipmap k v))) (mapmap inc {:a 0, :b 1}) [/code] Is there a more concise way to do this in Clojure? If not, is this some