| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Text.PrettyPrint.PrettyTable
Description
This module features methods that can be used to print values contained inside a list / a map / or a vector in a tabular format.
Example of output
ticker price marketCap "YHOO" 42.2910101 4.0e10 "GOOG" 774.210101 5.3209e11 "AMZN" 799.161717 3.7886e11
Basic Steps
- Declare the new data type that will be the element of a list, map or a vector.
- The type could be a basic data constructor or a record type
- Derive
GenericandDatafor the type - Make the type an instance of
Tabilize - Create values of the type and add them to a list, map or vector
- Use one of the
printList,printMapandprintVectormethods to print the values in a tabular format
Usage Example
-- Printing a list of records
:set -XDeriveGeneric
:set -XDeriveDataTypeable
import qualified GHC.Generics as G
import Data.Data
-- A record structure that will in a list
data Stock = Stock {ticker::String, price::Double, marketCap::Double} deriving (Data, G.Generic)
-- Create an instance of Tabilize
instance Tabilize Stock
let yahoo = Stock {ticker="YHOO", price=42.29101010, marketCap=40e9}
let google = Stock {ticker="GOOG", price=774.210101, marketCap=532.09e9}
let amazon = Stock {ticker="AMZN", price=799.161717, marketCap=378.86e9}
-- List of records
let tickers = [yahoo, google, amazon]
printList tickers
ticker price marketCap
"YHOO" 42.2910101 4.0e10
"GOOG" 774.210101 5.3209e11
"AMZN" 799.161717 3.7886e11Tabilize methods
class Data a => Tabilize a where Source #
Specialized class that provides default methods methods to print List, Map or Vector values as a pretty table
Methods
listToBox :: [a] -> [[Box]] Source #
Return a list of values wrapped in a Box. Each entry in input is assumed to be a list of records keyed by any data type. The first entry in the list of values will be used to infer the names of fields
mapToBox :: Show b => Map b a -> [[Box]] Source #
Return a list of values wrapped in Box. Each entry in input is assumed to be a list of records keyed by any data type. The first entry in the list of values will be used to infer the names of fields
vectorToBox :: Vector a -> [[Box]] Source #
Return a list of values wrapped in Box. Each entry in input is assumed to be a list of records keyed by any data type. The first entry in the list of values will be used to infer the names of fields
listToBox :: (Generic a, GTabilize (Rep a)) => [a] -> [[Box]] Source #
Return a list of values wrapped in a Box. Each entry in input is assumed to be a list of records keyed by any data type. The first entry in the list of values will be used to infer the names of fields
mapToBox :: (Generic a, GTabilize (Rep a), Show b) => Map b a -> [[Box]] Source #
Return a list of values wrapped in Box. Each entry in input is assumed to be a list of records keyed by any data type. The first entry in the list of values will be used to infer the names of fields
vectorToBox :: (Generic a, GTabilize (Rep a)) => Vector a -> [[Box]] Source #
Return a list of values wrapped in Box. Each entry in input is assumed to be a list of records keyed by any data type. The first entry in the list of values will be used to infer the names of fields
printMap :: Show b => Map b a -> IO () Source #
import qualified Data.Map as M
-- declare a Map
data Portfolio = M.Map String Stock
Add the Stock values we create
let p = M.fromList [("YHOO", yahoo), ("GOOG", google), ("AMZN" amazon)]
printMap p
Key ticker price marketCap
"amzn" "AMZN" 799.161717 3.7886e11
"goog" "GOOG" 774.210101 5.3209e11
"yhoo" "YHOO" 42.2910101 4.0e10printList :: [a] -> IO () Source #
-- List of records let tickers = [yahoo, google, amazon] printList tickers ticker price marketCap "YHOO" 42.2910101 4.0e10 "GOOG" 774.210101 5.3209e11 "AMZN" 799.161717 3.7886e11
printVector :: Vector a -> IO () Source #
import qualified Data.Vector as V -- Vector of records let tickers = V.fromList [yahoo, google, amazon] printVector tickers ticker price marketCap "YHOO" 42.2910101 4.0e10 "GOOG" 774.210101 5.3209e11 "AMZN" 799.161717 3.7886e11
Custom Rendering
In cases, where the printList, printMap or printVector statements are not adequate, the listToBox, vectorToBox and mapToBox methods
return the B.Box value. The user can then use some of the methods provided by
Text.PrettyPrint.Box module.