sequences.extras: adding pad-center.
parent
7ee4e5dea5
commit
d015f028db
|
@ -21,6 +21,21 @@ IN: sequences.extras.tests
|
|||
{ "abc" " " } [ "abc" "" CHAR: \s pad-longest ] unit-test
|
||||
{ "abc..." "foobar" } [ "abc" "foobar" CHAR: . pad-longest ] unit-test
|
||||
|
||||
{
|
||||
{
|
||||
"ABC"
|
||||
"ABC"
|
||||
"ABC"
|
||||
"ABC"
|
||||
"ABC-"
|
||||
"-ABC-"
|
||||
"-ABC--"
|
||||
"--ABC--"
|
||||
}
|
||||
} [
|
||||
"ABC" 8 iota [ CHAR: - pad-center ] with map
|
||||
] unit-test
|
||||
|
||||
{ { 0 1 0 1 } } [
|
||||
{ 0 0 0 0 } { 1 3 } over [ 1 + ] change-nths
|
||||
] unit-test
|
||||
|
|
|
@ -82,6 +82,13 @@ IN: sequences.extras
|
|||
: pad-longest ( seq1 seq2 elt -- seq1 seq2 )
|
||||
[ 2dup max-length ] dip [ pad-tail ] 2curry bi@ ;
|
||||
|
||||
:: pad-center ( seq n elt -- padded )
|
||||
n seq length [-] :> extra
|
||||
extra 2/ :> left
|
||||
extra left - :> right
|
||||
left elt <repetition> seq right elt <repetition>
|
||||
seq 3append-as ;
|
||||
|
||||
: change-nths ( ... indices seq quot: ( ... elt -- ... elt' ) -- ... )
|
||||
[ change-nth ] 2curry each ; inline
|
||||
|
||||
|
|
Loading…
Reference in New Issue