Skip to content

Instantly share code, notes, and snippets.

@joom
Created February 19, 2018 23:44
Show Gist options
  • Save joom/2094cbcf7da1f9808a32fbe6942c20f5 to your computer and use it in GitHub Desktop.
Save joom/2094cbcf7da1f9808a32fbe6942c20f5 to your computer and use it in GitHub Desktop.
module Table where
import Data.List
t1 :: [[String]]
t1 = [ ["name", "age"]
, ["Obama", "56"]
, ["Elizabeth", "91"]
, ["Josiah Edward \"Jed\" Bartlet", "76"]
]
-- | Print table with aligned fields, assuming a tab char completes to 8 chars
showTable :: [[String]] -> String
showTable t = intercalate "\n" . map
(\row -> concatMap
(\(field, n) -> field ++ replicate (n - div (length field) 8 + 1) '\t')
(zip row maxTabsPerField)) $ t
where maxTabsPerField =
map ((+1) . (`div` 8) -- get max # of tabs required
. maximum -- get max # of chars in the same column
. map length) -- get lengths of each field
. transpose $ t -- get columns in the same list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment