formatting-6.2.1: Combinator-based type-safe formatting (like printf() or FORMAT)

Copyright(c) 2013 Chris Done, 2013 Shachaf Ben-Kiki
LicenseBSD3
Maintainerchrisdone@gmail.com
Stabilityexperimental
PortabilityGHC
Safe HaskellNone
LanguageHaskell98

Formatting

Contents

Description

Combinator-based type-safe formatting (like printf() or FORMAT) for Text.

Example:

>>> format ("Person's name is " % text % ", age is " % hex) "Dave" 54

See Formatting.Formatters for a complete list of formatting combinators.

Synopsis

Documentation

data Format r a Source

A formatter. The r type means the returned value at the end. The more formatters you compose, the more this wil build up arguments from r to Int -> r to Char -> (Int -> r), etc.

Instances

Category * Format

The same as (%). At present using Category has an import overhead, but one day it might be imported as standard.

(~) * a r => IsString (Format r a)

Useful instance for writing format string. With this you can write Foo instead of now "Foo!".

Monoid (Format r (a -> r))

Useful instance for applying two formatters to the same input argument. For example: format (year <> "/" % month) now will yield "2015/01".

(%) :: Format r a -> Format r' r -> Format r' a infixr 9 Source

Composition operator. Format is an instance of Category, but that is (at present) inconvenient to use with regular Prelude. So this function is provided as a convenience.

(%.) :: Format r (Builder -> r') -> Format r' a -> Format r a infixr 8 Source

Function compose two formatters. Will feed the result of one formatter into another.

now :: Builder -> Format r r Source

Insert a constant monoidal value.

later :: (a -> Builder) -> Format r (a -> r) Source

Insert a function which accepts some argument and produces a Builder which is appended to the output at the end.

later (f :: Int -> Builder) produces Format r (Int -> r).

Top-level functions

format :: Format Text a -> a Source

Run the formatter and return a lazy Text value.

sformat :: Format Text a -> a Source

Run the formatter and return a strict Text value.

bprint :: Format Builder a -> a Source

Run the formatter and return a Builder value.

fprint :: Format (IO ()) a -> a Source

Run the formatter and print out the text to stdout.

hprint :: Handle -> Format (IO ()) a -> a Source

Run the formatter and put the output onto the given Handle.

Formatting library

Other functions

formatToString :: Format [Char] a -> a Source

Run the formatter and return a list of characters.