ClojureDocs

Nav

Namespaces

pprint

clojure.pprint

Available since 1.2
  • (pprint object)
  • (pprint object writer)
Pretty print object to the optional output writer. If the writer is not provided, 
print the object to the currently bound value of *out*.
6 Examples
(def big-map (zipmap 
                 [:a :b :c :d :e] 
                 (repeat 
                   (zipmap [:a :b :c :d :e] 
                      (take 5 (range))))))
;;=> #'user/big-map

big-map
;;=> {:a {:a 0, :b 1, :c 2, :d 3, :e 4}, :b {:a 0, :b 1, :c 2, :d 3, :e 4}, :c {:a 0, :b 1, :c 2, :d 3, :e 4}, :d {:a 0, :b 1, :c 2, :d 3, :e 4}, :e {:a 0, :b 1, :c 2, :d 3, :e 4}}

(clojure.pprint/pprint big-map)
;; {:e {:e 4, :d 3, :c 2, :b 1, :a 0},
;;  :d {:e 4, :d 3, :c 2, :b 1, :a 0},
;;  :c {:e 4, :d 3, :c 2, :b 1, :a 0},
;;  :b {:e 4, :d 3, :c 2, :b 1, :a 0},
;;  :a {:e 4, :d 3, :c 2, :b 1, :a 0}}
;; nil
;; suppose you want to pretty print to a file.
(clojure.pprint/pprint *map* (clojure.java.io/writer "foo.txt"))
;; writes the contents of *map* to a file named 'foo.txt'
;; pprint into a string using with-out-str
(with-out-str (clojure.pprint/pprint {:x 1 :y -1}))
;; => "{:x 1, :y -1}\n"

;; pprint into a string using StringWriter
(let [out (java.io.StringWriter.)]
  (clojure.pprint/pprint {:a 1 :b 2} out)
  (clojure.pprint/pprint {:c 3 :d 4} out)
  (.toString out))
;; => "{:a 1, :b 2}\n{:c 3, :d 4}\n"
;;how to use it with :require and :use

;; :require 
(ns example.pprinter
   (:require [clojure.pprint :as pp]))

(def myname "John Smith")
(pp/pprint myname)

--------------

;;:use
(ns example.pprinter
    (:use clojure.pprint))

(def myname "John Smith")
(pprint myname)
;; get Clojure snippets to print nicely
(require '[clojure.pprint :as p])

(def lost-formatting 
  "(defn op [sel] (condp = sel :plus + :minus - :mult * 
   :div / :rem rem :quot quot :mod mod))")

(p/with-pprint-dispatch
  p/code-dispatch
  (p/pprint 
    (clojure.edn/read-string lost-formatting)))

;; (defn op [sel]
;;   (condp = sel
;;     :plus +
;;     :minus -
;;     :mult *
;;     :div /
;;     :rem rem
;;     :quot quot
;;     :mod mod))
;; pprint with metadata, requires Clojure >= 1.10.2
(require '[clojure.pprint :as p])
(binding [*print-meta* true]
  (p/pprint ^{:hello :world} {}))
See Also

A convenience macro that pretty prints the last thing output. This is exactly equivalent to (pprint ...

Added by jafingerhut

Prints a collection of maps in a textual table. Prints table headings ks, and then a line of outp...

Added by jafingerhut

Same as pr followed by (newline). Observes *flush-on-newline*

Added by delonnewman

prn to a string, returning it

Added by delonnewman

Prints the object(s) to the output stream that is the current value of *out*. Prints the object(s...

Added by delonnewman

pr to a string, returning it

Added by delonnewman

Tab at this point in the pretty printing stream. kind specifies whether the tab is :line, :section, ...

Added by MicahElliott

The column at which to enter miser style. Depending on the dispatch table, miser style add newlines...

Added by MicahElliott

Pretty printing will try to avoid anything going beyond this column. Set it to nil to have pprint le...

Added by MicahElliott
1 Note
    By , created 6.6 years ago, updated 6.6 years ago

    When using Clojure from the REPL, you don't need to specify clojure.pprint to use pprint or pp. At the REPL (clojure.pprint x) is equivalent to (pprint x).