factor/basis/strings/tables/tables.factor

30 lines
729 B
Factor
Raw Normal View History

2010-01-14 10:10:13 -05:00
! Copyright (C) 2009, 2010 Slava Pestov.
2009-01-26 03:21:28 -05:00
! See http://factorcode.org/license.txt for BSD license.
USING: kernel sequences fry math.order splitting ;
2009-01-26 03:21:28 -05:00
IN: strings.tables
<PRIVATE
: map-last ( seq quot -- seq )
2010-01-14 10:10:13 -05:00
[ dup length iota <reversed> ] dip '[ 0 = @ ] 2map ; inline
: max-length ( seq -- n )
[ length ] [ max ] map-reduce ;
: format-row ( seq ? -- seq )
[
dup max-length
'[ _ "" pad-tail ] map
] unless ;
2009-01-26 03:21:28 -05:00
: format-column ( seq ? -- seq )
[
dup max-length
'[ _ CHAR: \s pad-tail ] map
2009-01-26 03:21:28 -05:00
] unless ;
PRIVATE>
: format-table ( table -- seq )
[ [ [ string-lines ] map ] dip format-row flip ] map-last concat
flip [ format-column ] map-last flip [ " " join ] map ;