Align columns: Difference between revisions

Content added Content deleted
(→‎{{header|Haskell}}: Removed second version for repair)
(→‎{{header|Haskell}}: Or, using Text and its functions as an alternative to [Char])
Line 2,495: Line 2,495:
Further, allow for each word in a column to be either left
Further, allow for each word in a column to be either left
justified, right justified, or center justified within its column.</pre>
justified, right justified, or center justified within its column.</pre>


Or, using '''Text''' and its functions as an alternative to '''[Char]''' strings:

<lang haskell>import Data.List (transpose, zip, maximumBy)
import Data.Text as T
(Text, pack, unpack, splitOn, unlines, unwords, length,
justifyLeft, justifyRight, center, replicate)
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."
]

intCols :: Int
intCols = maximum (Prelude.length <$> rows)

cols :: [[Text]]
cols =
transpose $
(\xs -> xs ++ Prelude.replicate (intCols - Prelude.length xs) (pack "")) <$> 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]</lang>
{{Out}}
<pre>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.

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.

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. </pre>


=={{header|HicEst}}==
=={{header|HicEst}}==