Created
February 19, 2018 23:44
-
-
Save joom/2094cbcf7da1f9808a32fbe6942c20f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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