On Nov 8, 2010, at 2:45 PM, Paul Mooser wrote: > One thing you could do is to use loop and recur as your looping > construct - every time you enter into the loop, your variable will be > re-bound to the value you used in recur - in this fashion, you can > avoid bashing the transient in place.
Yes, I tried to come up with a version that would do this (as this seems to be the standard way of using transients), but the algorithm doesn't seem to lend itself to this approach... - Greg > On Nov 8, 1:45 pm, Greg <g...@kinostudios.com> wrote: >> So... I tried, and failed, to use transients properly. >> >> The function below, new-gen, takes a sequence of "nodes" and creates a new >> generation of nodes out of them using the functions "transform-left" and >> "transform-right". >> >> Each node in the sequence has two children, one is created with >> transform-left, the other with tranform-right. >> >> The desired result is a new sequence containing all the children, >> representing the "new generation". >> >> The catch is that some of the children are aborted at birth according the >> following rules: >> >> 1) If the child has an existing parent with a value equal to its own, it is >> left out of the sequence >> 2) If the child has an existing sibling with a value equal to its own, it >> too is left out of the resultant sequence >> >> The function is listed below: >> >> (defn new-gen [a] >> (let [t (transient [])] >> (doseq [f a] >> (doseq [n [(transform-left f) (transform-right f)]] >> (when-not (contains? #(= (:val n) %) (map >> :val (parents n))) >> (conj! t n)))) ;; <-- transient >> bashing!! >> (distinct #(= (:val %1) (:val %2)) (persistent! t)))) >> >> I'm also using modified versions of 'distinct' and 'contains?', the full >> source for them (and the new-gen function) is available in >> syntax-hightlighted goodness here: >> >> http://paste.pocoo.org/show/288063/ >> >> The question is: how do I write new-gen in an elegant way and avoid "bashing >> the transient"? >> >> I must say it would be really nice if this was considered kosher.. as it >> does work just fine. It would also be useful to know why transient bashing >> is frowned upon. >> >> Any help is greatly appreciated! >> >> - Greg > > -- > 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 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