While Guillermo explained how to change the dispatch for pretty-printing in general, if all you want to do is printing one collection differently, that's possible, too.
For example, using cl-format
(after (use '[clojure.pprint :as pp)
):
(binding [pp/*print-pretty* true
pp/*print-miser-width* nil
pp/*print-right-margin* 10]
(pp/cl-format true "~<[~;~@{~a~^ ~:_~}~;]~:>~%" '[foo bar baz quux]))
Set *print-right-margin*
as you wish.
You don't have to use format for this. The format directives can be translated to their respective pretty-printer functions, if you want. Explanation of the format string: ~<
and ~:>
establish a logical block. Inside the block, there are three sections separated by ~;
. The first and last section are your prefix and suffix, while the elements are printed in the middle section, using ~@{
and ~}
. For each element, the element is printed using ~a
, followed by a space, if needed, and a conditional fill-style newline.
(In CL, the format string could be simplified to "~<[~;~@{~a~^ ~}~;]~:@>~%"
, but that doesn't seem to work in Clojure 1.5.)