Write can produce pretty output directly to a string and without the trailing newline, making this a little shorter:
(defn- apply-macro-expander [expander string] (binding [*print-suppress-namespaces* true] (with-pprint-dispatch *code-dispatch* (write (expander (read-from-string string)) :pretty true :stream nil)))) Completely untested! :-) Tom On Mar 12, 11:07 am, David Nolen <dnolen.li...@gmail.com> wrote: > I suppose the following is more idiomatic: > > (defn- apply-macro-expander [expander string] > (let [astr (with-out-str > (binding [*print-suppress-namespaces* true] > (with-pprint-dispatch *code-dispatch* > (pprint (expander (read-from-string string))))))] > (subs astr 0 (dec (count astr))))) > > > On Thu, Mar 12, 2009 at 12:53 PM, Tom Faulhaber > > <tomfaulha...@gmail.com>wrote: > > >> Expanding on David's earlier example of pretty printing, we can set > >> the dispatch to *code-dispatch* and bind *print-suppress-namespaces* > >> to true and get the following (apologies for google messing up my > >> formatting): > > >> (do > >> (defmacro dft2 [tree] > >> (seq (concat (list '=dft2) (list '*cont*) (list tree)))) > >> (defn =dft2 [*cont* tree] > >> (reset! *saved* nil) > >> (let [*cont* (fn > >> [node] > >> (cond > >> (= node 'done) (*cont* nil) > >> :else (do (print node) (restart))))] > >> (=dft-node *cont* tree)))) > > >> Which is not always what you want, since it loses the namespace info > >> that backquote adds, but for me it's usually what I want because it > >> looks like the code I would have written. > > >> A couple of notes for the detail-oriented: You can see the special > >> code formats for defmacro, defn, binding vectors, and cond in > >> operation here. You can also see that fn needs special treatment and > >> doesn't have it yet. (But it will, real soon now.) > > >> On Mar 12, 9:28 am, Tom Faulhaber <tomfaulha...@gmail.com> wrote: > >> > Rich, > > >> > I would be happy to make it a contribution (it's the least I can do!). > >> > I've had a CA sitting on my desk unread and unsigned for about 3 > >> > weeks. It is now read, signed, and in an envelope. I'll send it off > >> > this morning. > > >> > Everyone, > > >> > Thanks for the kind words. I'm glad you like it. > > >> > David's use case in slime/swank was one of the motivators for me > >> > writing this. Thanks for showing us how to do the integration, David! > >> > I hope we see a lot of other use cases like that. > > >> > Upcoming is the ability to create custom dispatch tables which will > >> > open the door to an even broader set of use cases. > > >> > On Mar 12, 1:56 am, Rich Hickey <richhic...@gmail.com> wrote: > > >> > > On Mar 12, 2009, at 3:05 AM, Tom Faulhaber wrote: > > >> > > > I have now "released" the first version of my pretty printer as part > >> > > > of my cl-format library. It is released under the EPL. > > >> > > > The pretty printer has two functions that you probably care about: > > >> > > > (pprint obj) will pretty print the given object, and > >> > > > (pp) at the REPL will pretty print the last result output, i.e. the > >> > > > value in *1. > > >> > > > The pretty printer currently supports two modes: simple and code. > >> > > > Simple mode prints structure in a standard way that's good for data. > >> > > > Code mode understands lots of Clojure forms (defn, binding vectors, > >> > > > condp, etc.) and attempts to print them in an idiomatic way. > > >> > > > Cl-format is on github athttp://github.com/tomfaulhaber/cl-format. > >> > > > There is a Readme there with instructions, examples, limitations and > >> > > > futures. I won't even try to put examples here, because google > >> groups > >> > > > wreaks havoc on formatting. > > >> > > > The simplest way to get some pretty printing happiness: > >> > > > 1) Download the jar: > >>http://github.com/tomfaulhaber/cl-format/raw/master/release/cl-format... > >> > > > 2) Put it in your classpath. > >> > > > 3) Fire up your REPL > >> > > > 4) (use 'com.infolace.format) > >> > > > 5) Use pprint and pp as described above. > > >> > > > This is definitely a first release and there are sure to be bugs. > >> And > >> > > > I know there are things missing. So let me know if you're having > >> > > > problems and I'll try to get things fixed up ASAP. > > >> > > > Enjoy! > > >> > > > Tom > > >> > > This looks very useful Tom. Would you consider contributing it to > >> > > Clojure? > > >> > > Rich --~--~---------~--~----~------------~-------~--~----~ 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 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 -~----------~----~----~----~------~----~------~--~---