Ignore this for now.
text aligment
Rosetta code poses this challenge:
Given a text file of many lines, where fields within a line are delineated by a single $ 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 aligned, right aligned, or centered within its column.
possible approaches
On the task's talk page, two possible approaches were outlined:
Using |. to rotate the text appropriately
Using 9!:17 to leverage J's built in alignment facilities.
This paper presents solutions modeled after each of these approaches.
preparation
Any solution will involve preparing the input first. The task provides the following sample input: {{{ text=: noun define 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. )}}}
Note that the input is inconsitent (some lines end with $, some do not), and the task specifically calls for handling this inconsistency.
Let us define a generic tool to cut on (multiple, nested) frets to obtain a (correspondingly multidimensional, rectangular) boxed array:
fretter =: (-@:(-:{:) }. <;._2@:,~)&>/ This is a monad whose input is a list of boxes; the last box contains the text to process, and each preceding box contains a fret (nesting proceding from right-to-left). Note that the <;._2@:,~ guaruntees the text always ends in a fret, and the (-@:(-:{:) }. compensates for a the trailing empty fields that may (or may not) have been introduced to make that guaruntee.
Now let's configure fretter for this task:
parse =: [: fretter '$' ; LF ; ]
And test it on our (inconsistent) sample input: {{{ parse text +
+
+
+
+
+
+
+
+
+
+
+
+ |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.| | | | +
+
+
+
+
+
+
+
+
+
+
+
+}}}
Great.
rotation
Rosetta code poses this:
