Re: Mapping a function to a map

2010-09-08 Thread Daniel Werner
On Sep 7, 9:00 am, Thomas thomas.g.kristen...@gmail.com wrote: I've also been using my own version of a map-to-values function extensively and it would be really nice to have something like that, either in contrib or in core. It comes in handy surprisingly often. +1 I find myself writing

Mapping a function to a map

2010-09-06 Thread Nicolas Oury
Dear all, is there a function to map a function to all values in a map, keeping the same keys? Reducing from the seqed map seems a bit slower that what could be done directly... Best, Nicolas. -- You received this message because you are subscribed to the Google Groups Clojure group. To post

Re: Mapping a function to a map

2010-09-06 Thread Sunil S Nandihalli
Nicolas I am not sure of the performance characteristics.. but you may want to look at *(clojure.walk/walk #(do (println inner : %) %) #(do (println outer : %) %) {:a 1 :b 2}) * Best regards, Sunil. On Mon, Sep 6, 2010 at 9:10 PM, Nicolas Oury nicolas.o...@gmail.com wrote: Dear all, is

Re: Mapping a function to a map

2010-09-06 Thread Sunil S Nandihalli
(clojure.walk/walk (fn [[key val]] [key (* 2 val)]) identity {:a 1 :b 2}) On Mon, Sep 6, 2010 at 9:26 PM, Sunil S Nandihalli sunil.nandiha...@gmail.com wrote: Nicolas I am not sure of the performance characteristics.. but you may want to look at *(clojure.walk/walk #(do (println inner :

Re: Mapping a function to a map

2010-09-06 Thread Robert McIntyre
walk is good but it's not lazy. If you want to preserve laziness you can do: (defn map-vals transform a map by mapping it's keys to different values. [f m] (into {} (map (fn [[key val]] [key (f val)]) m))) This is also nice because you can then write functions whose arguments are [val]

Re: Mapping a function to a map

2010-09-06 Thread Michał Marczyk
On 6 September 2010 18:29, Robert McIntyre r...@mit.edu wrote: walk is good but it's not lazy. If you want to preserve laziness you can do: This won't be lazy, because (into {} ...) is a strict operation. I'd suggest something like (defn mmap [f m] (zipmap (keys m) (map f (vals m f is

Re: Mapping a function to a map

2010-09-06 Thread Robert McIntyre
I thought that since into uses reduce, it would be lazy, but I was wrong. reduce just plows through everything with a non-lazy recursion. Why is reduce not lazy? --Robert McIntyre On Mon, Sep 6, 2010 at 12:37 PM, Michał Marczyk michal.marc...@gmail.com wrote: On 6 September 2010 18:29, Robert

Re: Mapping a function to a map

2010-09-06 Thread gary ng
On Mon, Sep 6, 2010 at 9:49 AM, Robert McIntyre r...@mit.edu wrote: I thought that since into uses reduce, it would be lazy, but I was wrong. reduce just plows through everything with a non-lazy recursion. Why is reduce not lazy? reduce in clojure == foldl in Haskell and as far as I know,

Re: Mapping a function to a map

2010-09-06 Thread Justin Kramer
reduce returns a single value; there's no collection to make lazy. There is reductions, which returns the intermediate results of reduce as a lazy sequence. Justin On Sep 6, 12:49 pm, Robert McIntyre r...@mit.edu wrote: I thought that since into uses reduce, it would be lazy, but I was wrong.

Re: Mapping a function to a map

2010-09-06 Thread gary ng
On Mon, Sep 6, 2010 at 10:09 AM, Justin Kramer jkkra...@gmail.com wrote: reduce returns a single value; there's no collection to make lazy. There is reductions, which returns the intermediate results of reduce as a lazy sequence. if f = cons in 'reduce f a seq', there is a collection. Though

Re: Mapping a function to a map

2010-09-06 Thread Michał Marczyk
On 6 September 2010 18:49, Robert McIntyre r...@mit.edu wrote: I thought that since into uses reduce, it would be lazy, but I was wrong. reduce just plows through everything with a non-lazy recursion. Well, there's another reason in that the concept of a lazy map is problematic. In Clojure, at

Re: Mapping a function to a map

2010-09-06 Thread Stuart Sierra
On Sep 6, 11:40 am, Nicolas Oury nicolas.o...@gmail.com wrote: is there a function to map a function to all values in a map, keeping the same keys? I like reduce because you can modify both the key and the value, and even choose to omit or add certain keys: (reduce (fn [[k v] m] (assoc m k

Re: Mapping a function to a map

2010-09-06 Thread Robert McIntyre
Why is it that clojure maps aren't lazy though? Wouldn't that be just as useful an abstraction as lazy sequences? Aren't map really just lists of pairs in the end anyway? --Robert McIntyre On Mon, Sep 6, 2010 at 1:19 PM, Michał Marczyk michal.marc...@gmail.com wrote: On 6 September 2010 18:49,

Re: Mapping a function to a map

2010-09-06 Thread gary ng
On Mon, Sep 6, 2010 at 10:47 AM, Robert McIntyre r...@mit.edu wrote: Why is it that clojure maps aren't lazy though? Wouldn't that be just as useful an abstraction as lazy sequences? Aren't map really just lists of pairs in the end anyway? how can laziness benefit map usage pattern ? efficient

Re: Mapping a function to a map

2010-09-06 Thread Nicolas Oury
Different reactions: 1. The reduce solution is O(n .log n) + time of mapping elements, as inserting in a map is O(log n). A tree with n leafs and arity at least binary is of size at most 2n. So a map on a map could be done in O(n) + time of mapping elements, but it would need a bit of support