Re: mapmap?

2010-12-09 Thread Ken Wesson
On Thu, Dec 9, 2010 at 8:43 PM, Steven E. Harris wrote: > Ken Wesson writes: > >> and to encapsulate as a function: >> >> (defn fmap [f m] >>   (into {} >>     (for [[k v] m] >>       [k (f v)]))) > > Here, "fmap" is a poor choice of name, if it's meant to be a reference > to Haskell's function o

Re: mapmap?

2010-12-09 Thread Steven E. Harris
Ken Wesson writes: > and to encapsulate as a function: > > (defn fmap [f m] > (into {} > (for [[k v] m] > [k (f v)]))) Here, "fmap" is a poor choice of name, if it's meant to be a reference to Haskell's function of the same name. It's not obvious to me that mapping a function over a

Re: mapmap?

2010-12-07 Thread javajosh
On Dec 7, 5:50 am, Sean Devlin wrote: > This is a solved problem.  The trick is to use a higher-higher order > function... > > http://fulldisclojure.blogspot.com/2010/01/12-fn-proposal-same-multis... Why not call it "unseq"? -- You received this message because you are subscribed to the Googl

Re: mapmap?

2010-12-07 Thread pepijn (aka fliebel)
If you use (empty m) rather than {} it will also work for other types. On Dec 7, 5:45 am, Ken Wesson wrote: > This is also very easy to implement: > > (into {} >   (for [[k v] the-map] >     [k (f v)])) > > e.g. > > user=> (into {} >   (for [[k v] {:a 3 :b 7}] >     [k (inc v)])) > {:a 4, :b 8} >

Re: mapmap?

2010-12-07 Thread Sean Devlin
This is a solved problem. The trick is to use a higher-higher order function... http://fulldisclojure.blogspot.com/2010/01/12-fn-proposal-same-multisame.html On Dec 7, 7:38 am, Daniel Janus wrote: > While on the topic, I'd like to raise a naming issue. > > The 'mapmap'

Re: mapmap?

2010-12-07 Thread Ken Wesson
On Tue, Dec 7, 2010 at 3:31 AM, Tobias Raeder wrote: > Just checking performance versus the version i have been using i > noticed quite a difference > > For 10 runs over a small 10 key/value map > (defn fmap [keyf valf m] >  (into {} >    (for [[k v] m] >      [(keyf k) (valf v)]))) > 550ms >

Re: mapmap?

2010-12-07 Thread Tobias Raeder
Just checking performance versus the version i have been using i noticed quite a difference For 10 runs over a small 10 key/value map (defn fmap [keyf valf m] (into {} (for [[k v] m] [(keyf k) (valf v)]))) 550ms (defn fmap2 [keyf valf m] (-> (fn [inputmap [k v]] (assoc inp

Re: mapmap?

2010-12-07 Thread Daniel Janus
While on the topic, I'd like to raise a naming issue. The 'mapmap' function seems to be a recurring theme (see, e.g., http://tech.puredanger.com/2010/09/24/meet-my-little-friend-mapmap/) and many Clojure projects include one -- Incanter comes to mind. My project used to, too. B

Re: mapmap?

2010-12-06 Thread Alex Baranosky
Ahh apply does it again! I saw how the code be simplified, but just tonight learned how apply can be used in situations like this. -- 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

Re: mapmap?

2010-12-06 Thread Ken Wesson
On Tue, Dec 7, 2010 at 12:04 AM, Alex Baranosky wrote: > I think it will be fun to come up with a solution that will work with n > maps: > (defn multi-fmap [f &maps] >    ...) > i.e. > (multi-fmap #(+ %1 %1 %2 ) {:a 1 :b 2} {:a 3 :b 4}) => {:a 5 :b 8} > (multi-fmap #(+ %1 %2 %3 ) {:a 1 :b 2} {:a 3

Re: mapmap?

2010-12-06 Thread Alex Baranosky
I think it will be fun to come up with a solution that will work with n maps: (defn multi-fmap [f &maps] ...) i.e. (multi-fmap #(+ %1 %1 %2 ) {:a 1 :b 2} {:a 3 :b 4}) => {:a 5 :b 8} (multi-fmap #(+ %1 %2 %3 ) {:a 1 :b 2} {:a 3 :b 4} {:a 5 :b 6}) => {:a 9 :b 12} -- You received this message

Re: mapmap?

2010-12-06 Thread Ken Wesson
This is also very easy to implement: (into {} (for [[k v] the-map] [k (f v)])) e.g. user=> (into {} (for [[k v] {:a 3 :b 7}] [k (inc v)])) {:a 4, :b 8} user=> and to encapsulate as a function: (defn fmap [f m] (into {} (for [[k v] m] [k (f v)]))) -- You received this

Re: mapmap?

2010-12-06 Thread Alex Baranosky
c 7, 2010 at 9:26 AM, Alex Baranosky < > alexander.barano...@gmail.com> wrote: > >> Oops. I meant: >> >> (mapmap inc {:a 1 :b 2}) => { :a 2 :b 3) >> >> Sorry about that. >> >> Alex >> >> -- >> You received this message beca

Re: mapmap?

2010-12-06 Thread Sunil S Nandihalli
Hi Alex, (clojure.contrib.generic.functor/fmap inc {:a 1 :b 2}) => {:a 2 :b 3} The above is probably what you want. HTH Sunil. On Tue, Dec 7, 2010 at 9:26 AM, Alex Baranosky < alexander.barano...@gmail.com> wrote: > Oops. I meant: > > (mapmap inc {:a 1 :b 2}) => { :a 2

Re: mapmap?

2010-12-06 Thread Alex Baranosky
Oops. I meant: (mapmap inc {:a 1 :b 2}) => { :a 2 :b 3) Sorry about that. Alex -- 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 moder

mapmap?

2010-12-06 Thread Alex Baranosky
Do the standard libraries contain a function to map a function over a map? (mapmap inc {:a 1 :b 2}) => (2, 3) -- 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 p

Re: mapmap

2009-12-29 Thread lambdatronic
I've been using a number of similar functions in my own coding. This was my approach. (defn seq2map "Constructs a map from a sequence by applying keyvalfn to each element of the sequence. keyvalfn should return a pair [key val] to be added to the map for each input sequence element." [

Re: mapmap

2009-12-20 Thread Konrad Hinsen
On 18 Dec 2009, at 15:03, Sean Devlin wrote: > The last entry is the most relevant to a map-utils library. Check out > the visitor stuff: > > http://groups.google.com/group/clojure-dev/msg/6c1bbce17cafdf52 > > The idea is to take your generic functor application idea and put it > on steroids. I'

Re: mapmap

2009-12-18 Thread Sean Devlin
Konrad, Yeah, there are discussions of two libs there. The idea is that most of the fn in this thread will be used for table-utils. The last entry is the most relevant to a map-utils library. Check out the visitor stuff: http://groups.google.com/group/clojure-dev/msg/6c1bbce17cafdf52 The idea

Re: mapmap

2009-12-18 Thread Konrad Hinsen
On 17 Dec 2009, at 20:26, Sean Devlin wrote: > Konrad, > I am working on a different proposal on the dev list: > > http://groups.google.com/group/clojure-dev/browse_thread/thread/9a518c853bfbba8b# > > This is a more in depth version of these functions for maps. I'd love > to have you contribute t

Re: mapmap

2009-12-17 Thread Alex Osborne
Avital Oliver writes: > My experience is that you should only use filter/map if you either have a > pre-existing function or you want to use a _very_ short closure (using #()). > For any other case the code becomes unmanageable and then I feel that using > for > is the wiser choice. I agree, bu

Re: mapmap

2009-12-17 Thread Avital Oliver
Erik, My experience is that you should only use filter/map if you either have a pre-existing function or you want to use a _very_ short closure (using #()). For any other case the code becomes unmanageable and then I feel that using for is the wiser choice. I hope that they are just as efficient,

Re: mapmap

2009-12-17 Thread Laurent PETIT
Florian Ebeling > > > > > I was just wondering how a #'map for maps could be done most > > > succinctly. Came up with this: > > > > > (defn mapmap > > > "Map values of map m using function f." > > > [f m] > > > (redu

Re: mapmap

2009-12-17 Thread Sean Devlin
Depends if you need a closure. There are times I do 2D mappings, and I have to write something like (map (partail map #(do-work %)) coll). Or I have to compose my mapping operations. for is awesome when you need some of the other built-ins, like :while Sean On Dec 17, 12:39 pm, Erik Price wr

Re: mapmap

2009-12-17 Thread Sean Devlin
Konrad, I am working on a different proposal on the dev list: http://groups.google.com/group/clojure-dev/browse_thread/thread/9a518c853bfbba8b# This is a more in depth version of these functions for maps. I'd love to have you contribute to the discussion here. Sean On Dec 17, 12:16 pm, Konrad

Re: mapmap

2009-12-17 Thread Erik Price
On Thu, Dec 17, 2009 at 12:16 PM, Konrad Hinsen wrote: > > On 17 Dec 2009, at 15:44, Sean Devlin wrote: > > (defn map-vals [f coll] > >  (into {} (map (juxt key (comp f val)) coll)) vs: > (defmethod fmap clojure.lang.IPersistentMap >   [f m] >   (into (empty m) (for [[k v] m] [k (f v)]))) Are

Re: mapmap

2009-12-17 Thread Konrad Hinsen
On 17 Dec 2009, at 15:44, Sean Devlin wrote: > 1. I'd call it map-vals (as opposed to map-keys) > > 2. Slight change in the body > > (defn map-vals [f coll] > (into {} (map (juxt key (comp f val)) coll)) > > There is talk of getting something like this into contrib. Stay > tuned :) There's alr

Re: mapmap

2009-12-17 Thread Christophe Grand
     (assoc! m k (f v))) (transient {}) m)) > > HTH, > > -- > Laurent > > 2009/12/17 C. Florian Ebeling > > > I was just wondering how a #'map for maps could be done most > > succinctly. Came up with this: > > > (defn mapmap > >  "Map values

Re: mapmap

2009-12-17 Thread Stuart Sierra
On Dec 17, 9:37 am, "C. Florian Ebeling" wrote: > (defn mapmap >   "Map values of map m using function f." >   [f m] >   (reduce (fn [m [k v]] >             (assoc m k (f v))) {} m)) > > But there is probably a more straightforward way. What do you use?

Re: mapmap

2009-12-17 Thread C. Florian Ebeling
> AFAIK, such a function is still not in clojure core because it's not clear > to Rich whether f should have one argument (the value), or 2 arguments (key > + value). That is a good question. But I thought if the key makes a difference, then one could use standard map and rely on map's collection

Re: mapmap

2009-12-17 Thread Laurent PETIT
[k v]] (assoc! m k (f v))) (transient {}) m)) HTH, -- Laurent 2009/12/17 C. Florian Ebeling > I was just wondering how a #'map for maps could be done most > succinctly. Came up with this: > > (defn mapmap > "Map values of map m using function f.&qu

Re: mapmap

2009-12-17 Thread Sean Devlin
e: > I was just wondering how a #'map for maps could be done most > succinctly. Came up with this: > > (defn mapmap >   "Map values of map m using function f." >   [f m] >   (reduce (fn [m [k v]] >             (assoc m k (f v))) {} m)) > > But there is proba

mapmap

2009-12-17 Thread C. Florian Ebeling
I was just wondering how a #'map for maps could be done most succinctly. Came up with this: (defn mapmap "Map values of map m using function f." [f m] (reduce (fn [m [k v]] (assoc m k (f v))) {} m)) But there is probably a more straightforward way. What do yo