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-to-map 1 2 3 4) (hash-map 1 2 3 4))
true

On Jun 18, 11:19 pm, Sean Devlin <[email protected]> wrote:
> 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", :c "C"}
>
> Notice the call to trans first, and then the result it applied to test-
> map.  This is because trans generates a closure.  In this case, it
> applies the count function to the map, and associates it with the
> key :count.
>
> Here's how I would write the incrementer:
>
> user=> ((trans :a #(inc (% :a))) test-map)
> {:a 1, :b "B", :c "C"}
>
> trans is a little cumbersome, generating a closure.  I also wrote a
> deftrans macro.  It creates a trans and stores it in the provided
> name:
>
> user=> (deftrans counter :count count)
> #'user/counter
>
> user=> (counter test-map)
> {:count 3, :a 0, :b "B", :c "C"}
>
> user=> (deftrans inc-a :a #(inc (% :a)))
> #'user/inc-a
>
> user=> (inc-a test-map)
> {:a 1, :b "B", :c "C"}
>
> Let's revisit the fact that trans generates a closure.  We can use the
> resulting transform anywhere we'd use a function.
>
> In a map...
>
> user=> (map (trans :count count) (repeat 5 test-map))
> ({:count 3, :a 0, :b "B", :c "C"}
> {:count 3, :a 0, :b "B", :c "C"}
> {:count 3, :a 0, :b "B", :c "C"}
> {:count 3, :a 0, :b "B", :c "C"}
> {:count 3, :a 0, :b "B", :c "C"})
>
> Or, we could use the def'd version
>
> user=> (map counter (repeat 5 test-map))
> (...)
>
> In a comp...
>
> user=> ((comp inc-a counter counter) test-map)
> {:count 4, :a 1, :b "B", :c "C"}
>
> and my personal favorite, in the STM...
>
> user=> (def test-ref (ref test-map))
> #'user/test-ref
>
> user=> (dosync(alter test-ref inc-a))
> {:a 1, :b "B", :c "C"}
>
> user=> @test-ref
> {:a 1, :b "B", :c "C"}
>
> Extra stuff
>
> I also added a feature to enable the use of decoder/lookup maps.  When
> a map is passed instead of a function, it is assumed that the map is
> to decode the specific key it is assigned to.
>
> (def decoder-map {0 "Awesome" 1 "Not Awesome"})
>
> ;This will decode the key :a
> user=> (deftrans decoder :a decoder-map)
> #'user/decoder
>
> user=> (decoder test-map)
> {:a "Awesome", :b "B", :c "C"}
>
> user=> (decoder @test-ref)
> {:a "Not Awesome", :b "B", :c "C"}
>
> That's it for now folks.  I leave it to you to consider what this is
> good for.  Personally, I like using this to help me transform database
> data.
>
> The code is here, as always:
>
> http://github.com/francoisdevlin/devlinsf-clojure-utils/tree/master
>
> Use the namespace lib.devlinsf.map-utils .  I'll push docs & test this
> evening.
>
> Happy hacking
>
> On Jun 18, 9:07 am, Chouser <[email protected]> wrote:
>
> > On Thu, Jun 18, 2009 at 1:21 AM, Rowdy Rednose<[email protected]> 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 recommendation against
> > using the names of built-in functions for locals and
> > arguments.  I know it can be hard to know about all the core
> > names, but both 'map' and 'keys' are functions commonly used
> > in operations like this.  I spent longer than I'd like to
> > admit staring at "(merge-with + map ..." trying to figure
> > out what merge-with was going to do with a second function
> > parameter.  When I re-use names like that I always end up
> > confusing myself eventually.
>
> > --Chouser
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to