liveBook · Manning

Pretty printing

As explained in Clojure: The Essential Reference

Pretty printing is the generation of human-readable textual representations with structured layout and spacing.

Pretty printing generates readable textual representations by adding layout, spacing, and line breaks beyond basic printing. In Clojure, clojure.pprint provides REPL-ready pprint and pp functions, plus write for configurable output. section 18.3

The main controls and applications include:

  • Use pprint or pp for readable Clojure data, and write when overriding options such as :stream, :base, :length, and :level. section 18.3
  • Limit collection size with *print-length* and nesting depth with *print-level*; truncated items appear as … and truncated branches as #. section 23.20
  • Select a dispatch strategy for the data being printed: simple-dispatch suits general structures, while code-dispatch formats Clojure source. section 18.3 section 23.20
  • Tune presentation with *print-pretty*, *print-right-margin*, *print-miser-width*, namespace suppression, metadata, numeric bases, and output streams. section 23.20
  • Use print-table to render maps or map sequences as aligned two-dimensional tables. section 18.3

Readable data, formatted code

A nested value such as {:a ["red" "blue" "green"], :b '(:north :south :east :west')} prints across several lines with pprint. For source code, binding clojure.pprint/code-dispatch around pprint preserves forms such as (condp = x 1 "one" ...) in a code-oriented layout instead of treating them as generic data. section 18.3 section 23.20

(def data {:a ["red" "blue" "green"]
           :b '(:north :south :east :west)
           :c {"x-axis" 1 "y-axis" 2}})

data ; #1
;; {:a ["red" "blue" "green"], :b (:north :south :east :west), :c {"x-axis" 1, "y-axis" 2}}

(pp) ; #2
;; {:a ["red" "blue" "green"],
;;  :b (:north :south :east :west),
;;  :c {"x-axis" 1, "y-axis" 2}}

(pprint data) ; #3
;; {:a ["red" "blue" "green"],
;;  :b (:north :south :east :west),
;;  :c {"x-axis" 1, "y-axis" 2}}

Compare ordinary data printing with pprint's added line breaks and indentation on the same nested value. section 18.3

How Clojure: The Essential Reference covers it

How deep it goes
Explains entry points, dispatch, dynamic controls, truncation, margins, streams, bases, and tables, without detailing printer internals.
Examples
Uses nested maps and collections, RSS XML data, Clojure forms, macroexpansion, numeric bases, output files, and printed tables.
What it assumes
Assumes familiarity with Clojure values and basic printing primitives such as pr and print.
What you can do after
Operate/configure clojure.pprint to display data, format code, constrain output, write streams, and render tables.
How it connects
Connects pretty printing with Clojure data structures, Clojure source code, dynamic variables, printing, and serialization.

Also explained in

Compare how 3 books explain pretty printing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.