lambdaway
::
align_columns
1
|
list
|
login
|
load
|
|
_h1 align columns _p Following [[https://rosettacode.org/wiki/Align_columns| https://rosettacode.org/wiki/Align_columns]]. _p The input data is a sequence of characters where words are supposed to be separated by {b $} and lines be ended by {b $\}. {prewrap '{def txt 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.} -> {def txt 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 '{def columns // function's name {def columns.r // loop function {lambda {:just :a :b} {if {A.empty? :a} // end of loop then :b // return the string else {columns.r :just // justification {A.rest :a}. // loop on next char {if {W.equal? {A.first :a} \} // if end of line then < tr> :b // open a table row else {if {W.equal? {A.first :a} $} // if space between words then < td style="text-align::just;">:b // open a table data with justif else {A.first :a}:b }} } }}} // else add character {lambda {:just :txt} // main function {table // open an HTML table {columns.r // call the loop function :just // justification {A.reverse {A.split ${:txt}}} // split and reverse . // end point }}}} -> {def columns {def columns.r {lambda {:just :a :b} {if {A.empty? :a} then :b else {columns.r :just {A.rest :a} {if {W.equal? {A.first :a} \} then
:b else {if {W.equal? {A.first :a} $} then
:b else {A.first :a}:b }} } }}} {lambda {:just :txt} {table {columns.r :just {A.reverse {A.split ${:txt}}} .}}}} '{columns left txt} -> {columns left txt} '{columns center txt} -> {columns center txt} '{columns right txt} -> {columns right txt} } {style #page_frame { width:700px; } td { border:1px solid #ccc; } }
lambdaway v.20211111