]> gitweb.factorcode.org Git - factor.git/commitdiff
sequences.extras: simplify pad-center, add docs.
authorJohn Benediktsson <mrjbq7@gmail.com>
Fri, 11 Dec 2020 01:12:20 +0000 (17:12 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Fri, 11 Dec 2020 01:12:20 +0000 (17:12 -0800)
extra/sequences/extras/extras-docs.factor
extra/sequences/extras/extras.factor

index 58dc7bba66facdee64bd04be90fd7ed70d1dd1c1..93d5dd711136055d4526d0ba63632d6242a5f344 100644 (file)
@@ -2,6 +2,11 @@ USING: arrays help.markup help.syntax kernel math multiline
 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 } }
index c20e3d448a07a63e4a5a359b24f129bb79cbfbb8..2746c0d7339dffc7041c00a373f1387cd2a07727 100644 (file)
@@ -61,12 +61,10 @@ 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 ;
+: 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