Comment

Comment by QuinnWilton

Philip Wadler has a seminal paper [0] on implementing pretty printers by modeling them using an algebra. There's implementations of the algorithm in most programming languages nowadays, and some state of the art advances that help with performance [1]

It's a very elegant algorithm, and is very pleasant to work. Elixir's formatter uses a variant of it [2], and it appears in the standard library [3]. I've personally made use of it to write a code formatter for GraphQL queries, and it was a very small amount of code for some powerful results.

Definitely take a look if you ever need to do something like this.

[0] https://homepages.inf.ed.ac.uk/wadler/papers/prettier/pretti...

[1] https://jyp.github.io/pdf/Prettiest.pdf

[2] http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.34.2...

[3] https://hexdocs.pm/elixir/Inspect.Algebra.html

parent QuinnWilton 6 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton