OK, as of clojure-contrib commit 0d29198..., *print-base* and *print- radix* are fully supported for the various integer classes and ratios in cl-format, write, and pprint.
e.g.: (binding [*print-base* 2, *print-radix* true] (pprint (range 4))) => (#b0 #b1 #b10 #b11) Please let me know if you see any problems. Enjoy! Tom On Jul 5, 7:20 pm, Parth <parth.malwan...@gmail.com> wrote: > On Jul 5, 9:02 pm, Tom Faulhaber <tomfaulha...@gmail.com> wrote: > > > Parth, > > > I've created a little wrapper as promised at:http://gist.github.com/141001. > > > It gives you my-pprint: > > > user> (binding [*print-base* 2] (my-pprint (range 10))) > > (0 1 10 11 100 101 110 111 1000 1001) > > nil > > user> > > Thanks Tom. This works beautifully :) > > Here is the example that earlier example in the thread: > > user=> (binding [wrap-base/*print-base* 16] (wrap-base/my-pprint > (decode :b32 (test-ops 3)))) > {:inst > {:prefix (), > :code (c7 45 f8 a 0 0 0), > :op :movl, > :args [{:type :Ev-mem, :arg {:reg :ebp, :disp -8}} a]}, > :more ()} > nil > user=> > > > While doing this, I realized to my horror that ~r doesn't do 0 > > correctly for non-standard bases. I'll fix that soon. > > > Also, I've started to implement *print-radix* and *print-base* for > > real in cl-format and the pretty printer, so those should be available > > soon. > > Fantastic ... pprint just keeps getting better :) > > Regards, > Parth > > > Enjoy, > > > Tom > > > On Jul 3, 3:16 am, Parth <parth.malwan...@gmail.com> wrote: > > > > On Jul 3, 11:25 am, Tom Faulhaber <tomfaulha...@gmail.com> wrote: > > > > > Parth, > > > > > I was thinking about this a little more today and I came up with a way > > > > to extend the pretty printer easily to support *print-radix* with a > > > > little wrapper. I'll try to get a chance to write it up for you > > > > tomorrow. > > > > > Tom > > > > Sounds perfect. Thanks very much :) > > > > Regards, > > > Parth > > > > > On Jul 2, 6:29 pm, Parth <parth.malwan...@gmail.com> wrote: > > > > > > On Jul 3, 6:15 am, Parth <parth.malwan...@gmail.com> wrote: > > > > > > > Tom, Chouser, Thanks for your responses. > > > > > > > As of now I am doing the same thing as suggested. > > > > > > However, this tends be become painful the moment structures > > > > > > start to nest. For e.g. I am using Clojure to decode a bit > > > > > > of assembly and below is what I end up doing to see the > > > > > > values of interest in hex: > > > > > > > user=> (decode :b32 (nth test-ops 3)) > > > > > > {:inst {:prefix (), :code (199 69 248 10 0 0 0), :op :movl, :args > > > > > > [{:type :Ev-mem, :arg {:reg :ebp, :disp -8}} 10]}, :more ()} > > > > > > user=> (def r (decode :b32 (nth test-ops 3))) > > > > > > #'user/r > > > > > > user=> (map hex (get-in r [:inst :code])) > > > > > > ("c7" "45" "f8" "a" "0" "0" "0") > > > > > > user=> (hex (second (get-in r [:inst :args]))) > > > > > > "a" > > > > > > user=> > > > > > > > Basically, I need to extract each number seq or value > > > > > > individually and print it in hex for every instruction I > > > > > > decode and view. > > > > > > > This isn't too much fun to do in the middle of a debug session :) > > > > > > > Having something like *print-base* would be ideal IMHO > > > > > > would make scenarios like this really easy as one could > > > > > > simply do: > > > > > > > user=> (set! *print-base* 16) > > > > > > user=> (decode :b32 (nth test-ops 3)) > > > > > > {:inst {:prefix (), :code (c7 47 f8 a 0 0 0), :op :movl, :args > > > > > > [{:type :Ev-mem, :arg {:reg :ebp, :disp f8}} a]}, :more ()} > > > > > > > In the absence of this I thought of writing a function > > > > > > that would take an arbitrary Clojure structure/coll and print > > > > > > it out in the manner like above. But then it won't > > > > > > be much different from pprint with radix support but without > > > > > > the pretty part. > > > > > > > I suppose what I am hoping is that a feature request for > > > > > > *print-base* sort of a mechanism get considered > > > > > > for Clojure as it makes scenarios like the above very > > > > > > easy to deal with. Any chance of this being somewhere > > > > > > on the Clojue todo? :) > > > > > > Rich, > > > > > > If this is something you think would be a good addition > > > > > to Clojure I could give a shot at creating a patch for > > > > > this (with a CA of course). Please let me know. > > > > > > I think rather than a generic radix support, if > > > > > we have hex, bin and octal supported, most uses > > > > > cases should be covered. > > > > > > Regards, > > > > > Parth > > > > > > > I will probably create a poor mans radix based print > > > > > > in the mean time for the this scenario. That should > > > > > > be an interesting exercise. > > > > > > > Thanks, > > > > > > Parth > > > > > > > On Jul 2, 10:58 pm, Chouser <chou...@gmail.com> wrote: > > > > > > > > On Thu, Jul 2, 2009 at 4:51 AM, Parth > > > > > > > > Malwankar<parth.malwan...@gmail.com> wrote: > > > > > > > > > I frequently deal with hex and binary numbers. > > > > > > > > As of now when I need to view a list of numbers > > > > > > > > I just map a little hex function to it to translate it > > > > > > > > into a list of hex strings at the repl. > > > > > > > > > Having something like *print-base* / *print-radix* [1] may be > > > > > > > > valuable in such a scenario > > > > > > > > I don't think Java's built-in formatter is nearly as > > > > > > > flexible as those, but getting hex or octal strings is easy > > > > > > > enough: > > > > > > > > user=> (format "%d" 255) > > > > > > > "255" > > > > > > > user=> (format "%o" 255) > > > > > > > "377" > > > > > > > user=> (format "%x" 255) > > > > > > > "ff" > > > > > > > user=> (format "%X" 255) > > > > > > > "FF" > > > > > > > > --Chouser --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---