(defn update-in!
"modified version of core/update-in that works on, and return
transients"
([m [k & ks] f & args]
(if ks
(assoc! m k (apply update-in! (get m k) ks f args))
(assoc! m k (apply f (get m k) args)))))
user=> (persistent!(update-in!(transient v) [0] reverse))
[(2 1) [3 4]]
BUT when using nested paths, it fails:
user=>(persistent!(update-in!(transient v) [0 0] inc))
java.lang.ClassCastException: clojure.lang.PersistentVector cannot be
cast to clojure.lang.ITransientAssociative
Any idea how to solve this?
So you've got a transient vector of persistent vectors of
numbers. The problem is your update-in! then calls assoc! on
each level, but of course assoc! on the inner persistent vector
fails.
You either need to make the inner vectors transient (and then
call persist! on them when you're done) or use assoc! only at the
outer level.
--Chouser
http://joyofclojure.com/
(defn update-in!!
"modified version of core/update-in that works on, and return
transiants"
([m [k & ks] f & args]
(if ks
(persistent!(assoc! (transient m) k (apply update-in!! (get m k)
ks f args)))
(persistent!(assoc! (transient m) k (apply f (get m k) args))))))
On Jan 17, 3:57 pm, Chouser <chou...@gmail.com> wrote:
Can you tell us more about your problem, what do those deeply nested
vectors represent and how are you going to update them? (are all
updates batched in one part of your program?)
With transients current implementation you can't write an efficient update-in!
Christophe
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@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+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
>
--
Professional: http://cgrand.net/ (fr)
On Clojure: http://clj-me.cgrand.net/ (en)
http://pasadena.wr.usgs.gov/office/baagaard/research/papers/thesis/figs/methods/sparseMatrix.gif
This should be fast as long as you're only updating. If you're
inserting/deleting, you might be able to get away with using a
collection of 1D trees.
Sean
On Jan 20, 4:59 pm, Sean Devlin <francoisdev...@gmail.com> wrote:
> Gabi,
> A similar technique is used with sparse matrices. You usually have
> severals arrays, one for the non-zero elements, and another one for
> indexing the column and a third for indexing the rows.
>
> http://pasadena.wr.usgs.gov/office/baagaard/research/papers/thesis/fi...
[[1 2] [3 4]] is represented by {[0 0] 1, [0 1] 2, [1 0] 3, [1 1] 4}
--
On Jan 20, 9:32 am, Gabi <bugspy...@gmail.com> wrote:
> I posted a question on SO about it. Interesting discussion:http://stackoverflow.com/questions/2102606/algorithm-to-implement-non...