(clojure.tools.macro/symbol-macrolet [P +, M -, T *] ...) The tools.macro code-walker is much smarter and more careful than any that you or I will ever write.
On Monday, January 14, 2013 3:31:40 AM UTC-8, Jim foo.bar wrote: > > Hi everyone, I hope you're all well! > > I recently faced a problem when i needed to receive some piece of code > and replace all the invalid symbols in it with the proper (existent) > ones. Imagine for example you have the following map from made-up > characters to real characters: > > (def reserved-chars > (zipmap [\P \M \T] > [\+ \- \*]) ) > > Now, I'm expecting code that includes the symbols P, M, T but before > evaluating that code I need to replace these non-valid symbols with the > corresponding symbols Clojure knows about (+, -, * respectively). I > could get around that by def-ing P, M & T to +, - & * so that Clojure > knows the symbols when the time comes to eval the fn. However, this > solution is not ideal when there are many such symbols. My second > thought was to create a macro that simply transforms the expressions > replacing all the 'bad' symbols with 'good' ones...This is what I came > up with: > > > (defmacro translate [& code] > `(let [code-string# (str '~@code) > fake-to-real# reserved-chars] > ;(eval > (read-string ;;returns persistent-list > (reduce-kv > (fn [s# k# v#] > (.replaceAll ^String s# (str k#) (str v#))) > code-string# fake-to-real#)))) > > As you can probably gather, this macro treats the expression as a string > and not as data...Even though it runs perfectly fine and gives the > correct output , I was wondering if any of you macro-gurus has any > better suggestions or alternatives....Alternatively, if you can think of > any cases that will break my little macro please share them... > > thanks in advance... > > Jim > -- 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