Re: Pretty-print with metadata

2012-01-22 Thread Tom Faulhaber
Chris, nice use of custom dispatch! I can't claim all the credit for
this mechanism. I based my work on the old XP system that has been
used in Lisp since the eighties. Standing on the shoulders of giants,
as it were. :)

It's been on my list to respect *print-meta* in the regular pprint
dispatch, but I'd like to improve the overall performance of pprint
(which is still slower than I'd like for large data structures) first.

In the meantime, this is a good solution.

Tom

On Jan 20, 2:51 pm, Chris Perkins  wrote:
> Good catch!  I was about to add this to my personal toolkit of "generally
> useful random crap" (every programmer has one of those, right?).  I'll make
> sure to cover that edge-case.  Thanks.
>
> - Chris

-- 
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


Re: Pretty-print with metadata

2012-01-20 Thread Chris Perkins
Good catch!  I was about to add this to my personal toolkit of "generally 
useful random crap" (every programmer has one of those, right?).  I'll make 
sure to cover that edge-case.  Thanks.

- Chris

-- 
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

Re: Pretty-print with metadata

2012-01-20 Thread Cedric Greevey
Yeah, but it won't quite work if even the metadata has metadata.

user=> (def x (with-meta {:b 1} (with-meta {:a 1} {:x 1})))
#'user/x
user=>
x
{:b 1}

user=>
(meta x)
{:a 1}

user=>
(meta (meta x))
{:x 1}

The code above would leave out the metadata {:x 1} on {:a 1}, though
it should display the metadata for anything *inside* that map.

I think it could be adjusted with:

(fn [o] ->(fn pm [o]

(orig-dispatch (meta o))->(pm (meta o))

Of course, this is probably a pretty rare corner case.

-- 
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


Re: Pretty-print with metadata

2012-01-20 Thread Meikel Brandmeyer (kotarak)
Cool. :) Good to hear, that things are working. Nice to have around.

Meikel

-- 
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

Re: Pretty-print with metadata

2012-01-20 Thread Chris Perkins
By looking at pprint.clj, I have come up with something that seems to work. 
No "hacking" is necessary - the code in pprint is impressively clear and 
extensible.  It's obviously designed to allow exactly this sort of 
extension to the printing mechanism.

user> (defn ppm [obj]
(let [orig-dispatch pprint/*print-pprint-dispatch*]
  (pprint/with-pprint-dispatch 
(fn [o]
  (when (meta o)
(print "^")
(orig-dispatch (meta o))
(pprint/pprint-newline :fill))
  (orig-dispatch o))
(pprint obj
#'user/ppm

user> (ppm elt)
^{:xmlns {"a" "http://aaa/"}, :xmlns-decls {"a" "http://aaa/"}}
{:tag :foo,
 :attrs {},
 :content
 [^{:prefix "a", :xmlns {"a" "http://aaa/"}}
  {:tag :bar, :attrs {}, :content [], :uri "http://aaa/"}
  ^{:xmlns {"a" "http://aaa/"}}
  {:tag :blah,
   :attrs {^{:prefix "a"}[:x "http://aaa/";] "y"},
   :content [],
   :uri nil}],
 :uri nil}
nil
user> 

Works great!  Tom F. is officially my clojure-hero-of-the-day.

thanks,

- Chris

-- 
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

Re: Pretty-print with metadata

2012-01-20 Thread Meikel Brandmeyer (kotarak)
Hi,

I don't know a built-in way. But that doesn't mean, that there isn't one. 
You could ask Tom Faulhaber directly. He did the pretty print stuff.

Sincerely
Meikel

-- 
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

Re: Pretty-print with metadata

2012-01-20 Thread Chris Perkins
Thanks Meikel.

So I guess from your reply that there is no built-in way to do this, right? 
 The objects I'm trying to inspect can be deeply nested maps and vectors, 
and I want to see all the metadata - not just on the root object.  I guess 
I would have to either re-implement some of the logic of pprint, or hack 
pprint itself to respect *print-meta*.

- Chris

-- 
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

Re: Pretty-print with metadata

2012-01-20 Thread Meikel Brandmeyer (kotarak)
Hi,

how about something like this?

(defn pprint-with-meta
  [thing]
  (when (instanceof? IMeta thing)
(print "^")
(pprint (meta thing))
(print " "))
  (pprint thing))

Sincerely
Meikel

-- 
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

Pretty-print with metadata

2012-01-20 Thread Chris Perkins
Is there a way to pretty-print an object with its metadata?

If I (set! *print-meta* true) at the REPL, I can see the metadata.  If I 
use (pprint thing), I can see the structure much more easily.  How can I do 
both?

- Chris

-- 
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