September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,26 @@
import Data.List (unfoldr, transpose)
import Control.Arrow (second)
dat =
"Given$a$text$file$of$many$lines,$where$fields$within$a$line$\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" ++
"column$are$separated$by$at$least$one$space.\n" ++
"Further,$allow$for$each$word$in$a$column$to$be$either$left$\n" ++
"justified,$right$justified,$or$center$justified$within$its$column.\n"
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)

View file

@ -0,0 +1,35 @@
import Prelude as P
import Data.Text as T
(Text, pack, unpack, splitOn, unlines, unwords, length,
justifyLeft, justifyRight, center)
import Data.List (transpose, zip, maximumBy)
import Data.Ord (comparing)
rows :: [[Text]]
rows =
(splitOn (pack "$") . pack) <$>
[ "Given$a$text$file$of$many$lines,$where$fields$within$a$line$"
, "are$delineated$by$a$single$'dollar'$character,$write$a$program"
, "that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$"
, "column$are$separated$by$at$least$one$space."
, "Further,$allow$for$each$word$in$a$column$to$be$either$left$"
, "justified,$right$justified,$or$center$justified$within$its$column."
]
cols :: [[Text]]
cols =
transpose $
((++) <*>
(flip P.replicate (pack []) .
(-) (maximum (P.length <$> rows)) . P.length)) <$>
rows
main :: IO ()
main =
mapM_ putStrLn $
[ (\cols f ->
(unpack . T.unlines) $
T.unwords <$> transpose ((\(xs, n) -> f (n + 1) ' ' <$> xs) <$> cols))
(zip cols ((T.length . maximumBy (comparing T.length)) <$> cols))
] <*>
[justifyLeft, justifyRight, center]

View file

@ -1,25 +0,0 @@
import Data.List
import Control.Monad
import Control.Arrow
dat = "Given$a$text$file$of$many$lines,$where$fields$within$a$line$\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" ++
"column$are$separated$by$at$least$one$space.\n" ++
"Further,$allow$for$each$word$in$a$column$to$be$either$left$\n" ++
"justified,$right$justified,$or$center$justified$within$its$column.\n"
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)