quotations sequences ;
IN: sequences.extras
+HELP: pad-center
+{ $values { "seq" sequence } { "n" "a non-negative integer" } { "elt" object } { "padded" "a new sequence" } }
+{ $description "Outputs a new sequence consisting of " { $snippet "seq" } " padded on the left and right with enough repetitions of " { $snippet "elt" } " to have the result be of length " { $snippet "n" } "." }
+{ $examples { $example "USING: io sequences ;" "{ \"ab\" \"quux\" } [ 5 CHAR: - pad-center print ] each" "-ab--\nquux-" } } ;
+
HELP: ?supremum
{ $values
{ "seq/f" { $maybe sequence } }
: 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 ;
+: pad-center ( seq n elt -- padded )
+ swap pick length [-] [ drop ] [
+ [ 2/ ] [ over - ] bi rot '[ _ <repetition> ] bi@ surround
+ ] if-zero ;
: change-nths ( ... indices seq quot: ( ... elt -- ... elt' ) -- ... )
[ change-nth ] 2curry each ; inline