If I understand right, I think you might be able to use the existing 
replace functions in clojure.walk. I find that this is about twice as fast:

(time
 (dotimes
   [_ 100000]
   (walk/postwalk-replace
     {"exp_1" 100 "exp_2" 20 "exp_3" 5}
     [:DIV [:ADD [:ID "P1" "exp_1"] [:ID "P2" "exp_2"] "exp_2419"] [:ID 
"P3" "exp_3"] "exp_2418"])))


Jony

On Wednesday, 3 December 2014 18:49:01 UTC, Dmitriy Morozov wrote:
>
> Hi, guys!
>
> I have an issue with my function where I use zippers to modify data 
> structure. The function modifies a tree (parsed grammar tree) to replace 
> expressions with values from a given map. Sadly, the algorithm I came up 
> with is pretty slow. I would appreciate if anyone can give me a hint on 
> what would be the right approach to do what I'm trying to accomplish. 
> Thanks!
>
> (defn- transform-values [parse-tree values-map]
>   "Replaces all expressions in parsed tree with values from a given map."
>   (loop [loc (zip/vector-zip parse-tree)]
>         (if (zip/end? loc)
>             (zip/root loc)
>           (if (zip/branch? loc)
>               (let [id (last (zip/children loc))]
>                 (if (contains? values-map id)
>                     (recur (zip/next (zip/replace loc (zip/node [(get 
> values-map id)]))))
>                   (recur (zip/next (zip/edit loc #(into [] (butlast %)))))))
>             (recur (zip/next loc)))
>           )))
> (time
>  (dotimes
>      [_ 100000]
>    (transform-values
>     [:DIV [:ADD [:ID "P1" "exp_1"] [:ID "P2" "exp_2"] "exp_2419"] [:ID "P3" 
> "exp_3"] "exp_2418"]
>     {"exp_1" 100 "exp_2" 20 "exp_3" 5})))
>  
> "Elapsed time: 2317.491 msecs" 
>
>
>
>

-- 
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/d/optout.

Reply via email to