Something like this seems a bit cleaner:

(defn apply-values
  "Applies f to every value in m and passes f zero or more args"
  [m f & args]
  (reduce (fn [accum [k v]]
               (assoc accum k (apply f v args))
              {}
              m))

and then:

(swap! *game-objects* apply-values update-object)

With this you get the bonus of a) cleaner code and b) being able to re-use
your apply-values function.

Code stolen from here:
http://blog.jayfields.com/2011/08/clojure-apply-function-to-each-value-of.html

Timothy



On Mon, Jan 21, 2013 at 1:54 PM, JvJ <[email protected]> wrote:

> Yeah I cought that.  Thanks, though.
>
>
> On Monday, 21 January 2013 15:35:24 UTC-5, Jim foo.bar wrote:
>>
>>  also, i'm sure it's a typo, but the fn you originally posted is missing
>> its argument vector...it won't even compile like that...
>>
>> Jim
>>
>>
>> On 21/01/13 20:33, JvJ wrote:
>>
>> That's it!  Thanks.
>>
>> On Monday, 21 January 2013 15:28:39 UTC-5, Jim foo.bar wrote:
>>>
>>> ...or you can go all the way, skipping reset! completely:
>>>
>>> (swap! game-objects (fn [objects] (reduce-kv #(assoc % %2 (update-object
>>> %3)) {} objects) ))
>>>
>>> Jim
>>>
>>> ps: I've not tested this but i don't see why this approach wouldn't
>>> work...
>>>
>>>
>>> On 21/01/13 20:24, Jim - FooBar(); wrote:
>>> > use reduce-kv on the original map to save some intermediate vector
>>> > allocation...something similar has come up recently...
>>> >
>>> > (reduce-kv #(assoc % %2 (update-object %3)) {} @game-objects)
>>> >
>>> > Jim
>>> >
>>> > On 21/01/13 20:21, JvJ wrote:
>>> >> I'm updating a set of objects stored in a map, and I feel like the
>>> >> way I'm doing it right now is inefficient.  Is there a better way?
>>> >>
>>> >> (defn update-all-objects
>>> >>   "Updates all game objects."
>>> >>   (reset! *game-objects*
>>> >>           (into {}
>>> >>                 (for [[k v] @*game-objects*]
>>> >>                   [k (update-object v)]))))
>>> >> --
>>> >> 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<http://groups.google.com/group/clojure?hl=en>
>>> >
>>>
>>>  --
>> 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
>> clojure+u...@**googlegroups.com
>> For more options, visit this group at
>> http://groups.google.com/**group/clojure?hl=en<http://groups.google.com/group/clojure?hl=en>
>>
>>
>>   --
> 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
>



-- 
“One of the main causes of the fall of the Roman Empire was that–lacking
zero–they had no way to indicate successful termination of their C
programs.”
(Robert Firth)

-- 
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