Re: changing a value in a vector of maps

2013-07-31 Thread Gary Johnson
Here's the code analogue of your find-assoc-in function for the approach 
Cedric is proposing. I actually came to the same solution before reading 
the responses to your post, so it's good to see that others also think this 
is a more memory efficient approach (obviously for larger vectors than the 
example).

(defn my-find-assoc-in
  [[test-key update-key] [test-val update-val] mapvec]
  (reduce
   (fn [v idx] (if (= (get-in v [idx test-key]) test-val)
   (assoc-in v [idx update-key] update-val)
 v))
   mapvec
   (range (count mapvec


Happy hacking,
  ~Gary

-- 
-- 
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 moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
Clojure group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: changing a value in a vector of maps

2013-07-31 Thread cej38
Friends,
  Thanks for the replies.  I will try your suggestions and get back to you 
if I have further questions.

-- 
-- 
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 moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
Clojure group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




changing a value in a vector of maps

2013-07-30 Thread cej38
Suppose I have a vector of maps (this could also be a vector of records)

(def aa [{:a 0 :b 0 :c 0} {:a 50 :b 0 :c 0} {:a 100 :b 0 :c 0}])

and I want to go in an change the value of associated with :c when :a has a 
value of 50, so that the final col of maps looks like

[{:a 0 :b 0 :c 0} {:a 50 :b 0 :c 3} {:a 100 :b 0 :c 0}].



I came up with a function that does this:

(defn find-assoc-in
  [k v  mv]
  (let [f #(if (= ((first k) %) (first v))
 (assoc % (second k) (second v))
 %)]
 (map f mv)))


(find-assoc-in [:a :c] [50 3] aa)


The problem with this function is that it potentially does a lot 
of unnecessary copying of elements that aren't changed, and thus has a lot 
of garbage collection.  I would like help coming up with something that 
does much less garbage collection.


Will someone please point out a better way of doing 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 posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
Clojure group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: changing a value in a vector of maps

2013-07-30 Thread Jim - FooBar();

Hi,

how about 

(vec
(for [{:keys [a b c] :as m} aa]
 (if (= a 50) (assoc m :c 3) m)))

:)
Jim


On 30/07/13 18:53, cej38 wrote:

Suppose I have a vector of maps (this could also be a vector of records)

(def aa [{:a 0 :b 0 :c 0} {:a 50 :b 0 :c 0} {:a 100 :b 0 :c 0}])

and I want to go in an change the value of associated with :c when :a 
has a value of 50, so that the final col of maps looks like


[{:a 0 :b 0 :c 0} {:a 50 :b 0 :c 3} {:a 100 :b 0 :c 0}].



I came up with a function that does this:

(defn find-assoc-in
  [k v  mv]
  (let [f #(if (= ((first k) %) (first v))
 (assoc % (second k) (second v))
 %)]
 (map f mv)))


(find-assoc-in [:a :c] [50 3] aa)


The problem with this function is that it potentially does a lot 
of unnecessary copying of elements that aren't changed, and thus has a 
lot of garbage collection.  I would like help coming up with something 
that does much less garbage collection.



Will someone please point out a better way of doing 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 posts from new members are moderated - please be patient 
with your first post.

To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google 
Groups Clojure group.
To unsubscribe from this group and stop receiving emails from it, send 
an email to clojure+unsubscr...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.




--
--
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 moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups Clojure group.

To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: changing a value in a vector of maps

2013-07-30 Thread Cedric Greevey
That's basically the same as the first solution, in terms of how it works
under the hood.

If the desire is for the output to share structure with the input, not just
the maps but the vector as well, then you need to do something a bit more
complicated. First you need to find the map you want, and return its index
in the vector (assuming it will be unique if present). Then you need to use
assoc-in with the index, like (assoc-in v [idx :c] 50) or whatever.

If there may be more than one map that should be adjusted, you could return
a seq of indices and repeatedly assoc-in, but I doubt this would give
superior efficiency unless the vector was typically huge and the number of
maps needing changes typically much smaller. With million-element vectors
and two or three maps needing updating it would likely be preferable to the
(vec (for ...)) approach. With dozen-element vectors with half a dozen maps
needing updating I'd go with (vec (for ...)).



On Tue, Jul 30, 2013 at 4:58 PM, Jim - FooBar(); jimpil1...@gmail.comwrote:

  Hi,

 how about 

 (vec
 (for [{:keys [a b c] :as m} aa]
  (if (= a 50) (assoc m :c 3) m)))

 :)
 Jim



 On 30/07/13 18:53, cej38 wrote:

 Suppose I have a vector of maps (this could also be a vector of records)

  (def aa [{:a 0 :b 0 :c 0} {:a 50 :b 0 :c 0} {:a 100 :b 0 :c 0}])

  and I want to go in an change the value of associated with :c when :a
 has a value of 50, so that the final col of maps looks like

  [{:a 0 :b 0 :c 0} {:a 50 :b 0 :c 3} {:a 100 :b 0 :c 0}].



  I came up with a function that does this:

  (defn find-assoc-in
   [k v  mv]
   (let [f #(if (= ((first k) %) (first v))
  (assoc % (second k) (second v))
  %)]
  (map f mv)))


  (find-assoc-in [:a :c] [50 3] aa)


  The problem with this function is that it potentially does a lot
 of unnecessary copying of elements that aren't changed, and thus has a lot
 of garbage collection.  I would like help coming up with something that
 does much less garbage collection.


  Will someone please point out a better way of doing 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 posts from new members are moderated - please be patient with
 your first post.
 To unsubscribe from this group, send email to
 clojure+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en
 ---
 You received this message because you are subscribed to the Google Groups
 Clojure group.
 To unsubscribe from this group and stop receiving emails from it, send an
 email to clojure+unsubscr...@googlegroups.com.
 For more options, visit https://groups.google.com/groups/opt_out.




  --
 --
 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 moderated - please be patient with
 your first post.
 To unsubscribe from this group, send email to
 clojure+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en
 ---
 You received this message because you are subscribed to the Google Groups
 Clojure group.
 To unsubscribe from this group and stop receiving emails from it, send an
 email to clojure+unsubscr...@googlegroups.com.
 For more options, visit https://groups.google.com/groups/opt_out.




-- 
-- 
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 moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
Clojure group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: changing a value in a vector of maps

2013-07-30 Thread Cedric Greevey
Note: to repeatedly assoc-in over a seq of indices you'd want

(reduce #(assoc-in %1 [%2 :c] 50) input-vec index-seq)

Replace the key :c and value 50 as needed. If the 50 would actually be
computed from the index, an expression using %2 would work. If the 50 would
actually be computed from the existing value associated with the :c (or
whatever) key, use update-in instead of assoc-in and pass in the function
that transforms the existing value to the appropriate new value as the last
argument to update-in.



On Tue, Jul 30, 2013 at 6:27 PM, Cedric Greevey cgree...@gmail.com wrote:

 That's basically the same as the first solution, in terms of how it works
 under the hood.

 If the desire is for the output to share structure with the input, not
 just the maps but the vector as well, then you need to do something a bit
 more complicated. First you need to find the map you want, and return its
 index in the vector (assuming it will be unique if present). Then you need
 to use assoc-in with the index, like (assoc-in v [idx :c] 50) or whatever.

 If there may be more than one map that should be adjusted, you could
 return a seq of indices and repeatedly assoc-in, but I doubt this would
 give superior efficiency unless the vector was typically huge and the
 number of maps needing changes typically much smaller. With million-element
 vectors and two or three maps needing updating it would likely be
 preferable to the (vec (for ...)) approach. With dozen-element vectors with
 half a dozen maps needing updating I'd go with (vec (for ...)).



 On Tue, Jul 30, 2013 at 4:58 PM, Jim - FooBar(); jimpil1...@gmail.comwrote:

  Hi,

 how about 

 (vec
 (for [{:keys [a b c] :as m} aa]
  (if (= a 50) (assoc m :c 3) m)))

 :)
 Jim



 On 30/07/13 18:53, cej38 wrote:

 Suppose I have a vector of maps (this could also be a vector of records)

  (def aa [{:a 0 :b 0 :c 0} {:a 50 :b 0 :c 0} {:a 100 :b 0 :c 0}])

  and I want to go in an change the value of associated with :c when :a
 has a value of 50, so that the final col of maps looks like

  [{:a 0 :b 0 :c 0} {:a 50 :b 0 :c 3} {:a 100 :b 0 :c 0}].



  I came up with a function that does this:

  (defn find-assoc-in
   [k v  mv]
   (let [f #(if (= ((first k) %) (first v))
  (assoc % (second k) (second v))
  %)]
  (map f mv)))


  (find-assoc-in [:a :c] [50 3] aa)


  The problem with this function is that it potentially does a lot
 of unnecessary copying of elements that aren't changed, and thus has a lot
 of garbage collection.  I would like help coming up with something that
 does much less garbage collection.


  Will someone please point out a better way of doing 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 posts from new members are moderated - please be patient with
 your first post.
 To unsubscribe from this group, send email to
 clojure+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en
 ---
 You received this message because you are subscribed to the Google Groups
 Clojure group.
 To unsubscribe from this group and stop receiving emails from it, send an
 email to clojure+unsubscr...@googlegroups.com.
 For more options, visit https://groups.google.com/groups/opt_out.




  --
 --
 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 moderated - please be patient with
 your first post.
 To unsubscribe from this group, send email to
 clojure+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en
 ---
 You received this message because you are subscribed to the Google Groups
 Clojure group.
 To unsubscribe from this group and stop receiving emails from it, send an
 email to clojure+unsubscr...@googlegroups.com.
 For more options, visit https://groups.google.com/groups/opt_out.






-- 
-- 
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 moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
Clojure group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: changing a value in a vector of maps

2013-07-30 Thread Cedric Greevey
For a more quantitative comparison, the (reduce #(assoc-in ...)) approach
generates O(* (count index-seq) (log_32 (count input-vec))) garbage, while
the (vec (for ...)) approach, with its temporary seq the size of the
vector, and likely wholesale replacement of the vector itself (assuming you
don't keep the old version around after this), generates O(count input-vec)
garbage. They both only traverse the whole vector once, copying in one case
and building up the index-seq in the other, so they're much more even in
speed however large the vector is (both O(count input-vec)).

This is why the assoc-in approach is only really better if the vector is
considerably larger than the number of entries needing updating. Otherwise,
the (vec (for ...)) approach produces simpler, more maintainable code and
should be preferred if you never expect the vectors to grow very large, or
if you expect the proportion of items needing updating to often be a
substantial fraction of all of them.


On Tue, Jul 30, 2013 at 6:34 PM, Cedric Greevey cgree...@gmail.com wrote:

 Note: to repeatedly assoc-in over a seq of indices you'd want

 (reduce #(assoc-in %1 [%2 :c] 50) input-vec index-seq)

 Replace the key :c and value 50 as needed. If the 50 would actually be
 computed from the index, an expression using %2 would work. If the 50 would
 actually be computed from the existing value associated with the :c (or
 whatever) key, use update-in instead of assoc-in and pass in the function
 that transforms the existing value to the appropriate new value as the last
 argument to update-in.



 On Tue, Jul 30, 2013 at 6:27 PM, Cedric Greevey cgree...@gmail.comwrote:

 That's basically the same as the first solution, in terms of how it works
 under the hood.

 If the desire is for the output to share structure with the input, not
 just the maps but the vector as well, then you need to do something a bit
 more complicated. First you need to find the map you want, and return its
 index in the vector (assuming it will be unique if present). Then you need
 to use assoc-in with the index, like (assoc-in v [idx :c] 50) or whatever.

 If there may be more than one map that should be adjusted, you could
 return a seq of indices and repeatedly assoc-in, but I doubt this would
 give superior efficiency unless the vector was typically huge and the
 number of maps needing changes typically much smaller. With million-element
 vectors and two or three maps needing updating it would likely be
 preferable to the (vec (for ...)) approach. With dozen-element vectors with
 half a dozen maps needing updating I'd go with (vec (for ...)).



 On Tue, Jul 30, 2013 at 4:58 PM, Jim - FooBar(); jimpil1...@gmail.comwrote:

  Hi,

 how about 

 (vec
 (for [{:keys [a b c] :as m} aa]
  (if (= a 50) (assoc m :c 3) m)))

 :)
 Jim



 On 30/07/13 18:53, cej38 wrote:

 Suppose I have a vector of maps (this could also be a vector of records)

  (def aa [{:a 0 :b 0 :c 0} {:a 50 :b 0 :c 0} {:a 100 :b 0 :c 0}])

  and I want to go in an change the value of associated with :c when :a
 has a value of 50, so that the final col of maps looks like

  [{:a 0 :b 0 :c 0} {:a 50 :b 0 :c 3} {:a 100 :b 0 :c 0}].



  I came up with a function that does this:

  (defn find-assoc-in
   [k v  mv]
   (let [f #(if (= ((first k) %) (first v))
  (assoc % (second k) (second v))
  %)]
  (map f mv)))


  (find-assoc-in [:a :c] [50 3] aa)


  The problem with this function is that it potentially does a lot
 of unnecessary copying of elements that aren't changed, and thus has a lot
 of garbage collection.  I would like help coming up with something that
 does much less garbage collection.


  Will someone please point out a better way of doing 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 posts from new members are moderated - please be patient with
 your first post.
 To unsubscribe from this group, send email to
 clojure+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en
 ---
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To unsubscribe from this group and stop receiving emails from it, send
 an email to clojure+unsubscr...@googlegroups.com.
 For more options, visit https://groups.google.com/groups/opt_out.




  --
 --
 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 moderated - please be patient with
 your first post.
 To unsubscribe from this group, send email to
 clojure+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en
 ---
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To 

Re: changing a value in a vector of maps

2013-07-30 Thread Joel Holdbrooks
This probably won't help with your garbage collection worries, but it's a 
bit more flexible.

*(defn find-assoc-in [pred coll k v  kvs]*
*  (let [f #(if (pred %) (apply assoc % k v kvs) %)]*
*(into (empty coll) (map f coll*


On Tuesday, July 30, 2013 10:53:48 AM UTC-7, cej38 wrote:

 Suppose I have a vector of maps (this could also be a vector of records)

 (def aa [{:a 0 :b 0 :c 0} {:a 50 :b 0 :c 0} {:a 100 :b 0 :c 0}])

 and I want to go in an change the value of associated with :c when :a has 
 a value of 50, so that the final col of maps looks like

 [{:a 0 :b 0 :c 0} {:a 50 :b 0 :c 3} {:a 100 :b 0 :c 0}].



 I came up with a function that does this:

 (defn find-assoc-in
   [k v  mv]
   (let [f #(if (= ((first k) %) (first v))
  (assoc % (second k) (second v))
  %)]
  (map f mv)))


 (find-assoc-in [:a :c] [50 3] aa)


 The problem with this function is that it potentially does a lot 
 of unnecessary copying of elements that aren't changed, and thus has a lot 
 of garbage collection.  I would like help coming up with something that 
 does much less garbage collection.


 Will someone please point out a better way of doing 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 posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
Clojure group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.