Mikhail Vorozhtsov | 22 Mar 09:46 2013

ANN: text-printer - abstract interface for text builders/printers


I was writing a library for working with IP addresses when I found 
myself puzzled with the number of contexts in which the textual 
representation of an address could be used: plain strings, bytestring 
builders (ASCII/UTF8), text builders, pretty printers, etc. I could've 
just written an `addressToString :: Address -> String` function, but 
that would be suboptimal: (a) namespace pollution (that's a lot of 
*ToString's if you count IPv4/6, network addresses, socket addresses, 
etc) and (b) some contexts can take advantage of the fact that textual 
representations are ASCII (e.g. UTF8 bytestring builder).

And so the text-printer[1] was born. It is mainly two type classes. One 
for injecting text into a monoid, with special methods for ASCII and 
UTF-8 characters/strings. The other provides the default injection for 
values of a type (think of the `Pretty` type class in pretty printing 
libraries), the textual representation is supposed to be simple 
(single-line). Plus some convenient combinators and number formatters.

[1] http://hackage.haskell.org/package/text-printer