Align columns: Difference between revisions

m
→‎{{header|Haskell}}: (Minor linting and import pruning of existing example)
m (→‎{{header|Haskell}}: (Minor linting and import pruning of existing example))
Line 2,461:
 
=={{header|Haskell}}==
<lang haskell>import Data.List (unfoldr, transpose)
import Control.MonadArrow (second)
import Control.Arrow
 
dat =
dat = "Given$a$text$file$of$many$lines,$where$fields$within$a$line$\n" ++
"areGiven$delineateda$bytext$afile$singleof$'dollar'many$characterlines,$writewhere$fields$within$a$programline$\n" ++
"are$delineated$by$a$single$'dollar'$character,$write$a$program\n" ++
"that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$\n" ++
"that$aligns$each$column$areof$separatedfields$by$atensuring$that$words$leastin$oneeach$space.\n" ++
"Further,column$alloware$for$each$word$in$a$columnseparated$toby$beat$eitherleast$leftone$space.\n" ++
"justifiedFurther,$rightallow$justified,for$oreach$centerword$justifiedin$within$itsa$column.$to$be$either$left$\n" ++
"justified,$right$justified,$or$center$justified$within$its$column.\n"
 
brkdwn =
brkdwn = takeWhile (not . null) . unfoldr (Just . second (drop 1) . span ('$' /=))
 
format j ls = map (unwords . zipWith align colw) rows
where
rows = map brkdwn $ lines ls
colw = map (maximum . map length) . transpose $ rows
align cw w =
case j of
'c' -> (replicate l ' ') ++ w ++ (replicate r ' ')
'r' -> (replicate dl ' ') ++ w
'l' -> w ++ (replicate dl ' ')
where
dl = cw - length w
(l, r) = (dl `div` 2, dl - l)</lang>
{{out}}
<pre>
9,659

edits