Align columns: Difference between revisions
Content added Content deleted
Drkameleon (talk | contribs) |
(add Standard ML) |
||
Line 7,654: | Line 7,654: | ||
|justified,| right |justified,| or |center|justified| within | its |column.| | | | |
|justified,| right |justified,| or |center|justified| within | its |column.| | | | |
||
+----------+----------+----------+------+------+---------+----------+--------+-------+-------+------+----+</pre> |
+----------+----------+----------+------+------+---------+----------+--------+-------+-------+------+----+</pre> |
||
=={{header|Standard ML}}== |
|||
<lang sml>fun curry f x y = f (x, y) |
|||
fun uncurry f (x, y) = f x y |
|||
fun maxWidths ([], widths) = widths |
|||
| maxWidths (strings, []) = map size strings |
|||
| maxWidths (s :: ss, w :: ws) = Int.max (size s, w) :: maxWidths (ss, ws) |
|||
val alignL = uncurry (StringCvt.padRight #" ") |
|||
and alignR = uncurry (StringCvt.padLeft #" ") |
|||
fun alignC (w, s) = |
|||
alignL (w, alignR ((w + size s) div 2, s)) |
|||
fun formatTable tab = |
|||
let |
|||
val columnWidths = foldl maxWidths [] tab |
|||
in |
|||
fn f => String.concatWith "\n" |
|||
(map (String.concatWith " " o curry (ListPair.map f) columnWidths) tab) |
|||
end |
|||
val readTable = |
|||
map (String.fields (curry op= #"$")) |
|||
o String.tokens (curry op= #"\n") |
|||
o TextIO.inputAll |
|||
(* test stdin with all alignments *) |
|||
val () = print (String.concatWith "\n\n" |
|||
(map (formatTable (readTable TextIO.stdIn)) [alignL, alignC, alignR]) ^ "\n")</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|Swift}}== |
=={{header|Swift}}== |