pad-left/right now work with any sequence

slava 2006-09-07 21:14:08 +00:00
parent 4ed790c2b6
commit 8a0d2e90d9
5 changed files with 20 additions and 20 deletions

View File

@ -36,7 +36,7 @@ $terpri
ARTICLE: "cookbook-colon-defs" "Shuffle word and definition cookbook" ARTICLE: "cookbook-colon-defs" "Shuffle word and definition cookbook"
"The " { $link dup } " word makes a copy of the value at the top of the stack:" "The " { $link dup } " word makes a copy of the value at the top of the stack:"
{ $example "5 dup ." "25" } { $example "5 dup * ." "25" }
"The " { $link sq } " word is actually defined as follows:" "The " { $link sq } " word is actually defined as follows:"
{ $code ": sq dup * ;" } { $code ": sq dup * ;" }
"(You could have looked this up yourself by clicking on the " { $link sq } " word itself.)" "(You could have looked this up yourself by clicking on the " { $link sq } " word itself.)"

View File

@ -124,6 +124,15 @@ M: object like drop ;
: last/first ( seq -- pair ) dup peek swap first 2array ; : last/first ( seq -- pair ) dup peek swap first 2array ;
: padding ( seq n elt -- newseq )
>r swap length [-] r> <array> ;
: pad-left ( seq n elt -- padded )
pick >r pick >r padding r> append r> like ;
: pad-right ( seq n elt -- padded )
pick >r padding r> swap append ;
: sequence= ( seq1 seq2 -- ? ) : sequence= ( seq1 seq2 -- ? )
2dup [ length ] 2apply tuck number= 2dup [ length ] 2apply tuck number=
[ (mismatch) -1 number= ] [ 3drop f ] if ; inline [ (mismatch) -1 number= ] [ 3drop f ] if ; inline

View File

@ -196,6 +196,16 @@ HELP: last/first
{ $values { "seq" "a sequence" } { "pair" "a two-element array" } } { $values { "seq" "a sequence" } { "pair" "a two-element array" } }
{ $description "Creates an array holding the first and last element of the sequence." } ; { $description "Creates an array holding the first and last element of the sequence." } ;
HELP: pad-left
{ $values { "seq" "a sequence" } { "n" "a non-negative integer" } { "elt" "an object"} { "padded" "a new sequence" } }
{ $description "Outputs a new sequence consisting of " { $snippet "seq" } " padded on the left with enough repetitions of " { $snippet "elt" } " to have the result be of length " { $snippet "n" } "." }
{ $examples { $example "{ \"ab\" \"quux\" } [ 5 CHAR: - pad-left print ] each" "---ab\n-quux" } } ;
HELP: pad-right
{ $values { "seq" "a sequence" } { "n" "a non-negative integer" } { "elt" "an object"} { "padded" "a new sequence" } }
{ $description "Outputs a new sequence consisting of " { $snippet "seq" } " padded on the right with enough repetitions of " { $snippet "elt" } " to have the result be of length " { $snippet "n" } "." }
{ $examples { $example "{ \"ab\" \"quux\" } [ 5 CHAR: - pad-right print ] each" "ab---\nquux-" } } ;
HELP: sequence= HELP: sequence=
{ $values { "seq1" "a sequence" } { "seq2" "a sequence" } { "?" "a boolean" } } { $values { "seq1" "a sequence" } { "seq2" "a sequence" } { "?" "a boolean" } }
{ $description "Tests if the two sequences have the same length and elements. This is weaker than " { $link = } ", since it does not ensure that the sequences are instances of the same class." } ; { $description "Tests if the two sequences have the same length and elements. This is weaker than " { $link = } ", since it does not ensure that the sequences are instances of the same class." } ;

View File

@ -48,15 +48,6 @@ UNION: alpha Letter digit ;
: >lower ( str -- lower ) [ ch>lower ] map ; : >lower ( str -- lower ) [ ch>lower ] map ;
: >upper ( str -- upper ) [ ch>upper ] map ; : >upper ( str -- upper ) [ ch>upper ] map ;
: padding ( str n ch -- padstr )
>r swap length [-] r> <string> ;
: pad-left ( str n ch -- padded )
pick >r padding r> append ;
: pad-right ( str n ch -- padded )
pick >r padding r> swap append ;
: ch>string ( ch -- str ) 1 swap <string> ; : ch>string ( ch -- str ) 1 swap <string> ;
: >string ( seq -- str ) : >string ( seq -- str )

View File

@ -66,16 +66,6 @@ HELP: padding
{ $values { "str" "a string" } { "n" "a non-negative integer" } { "ch" "a character"} { "padstr" "a new string" } } { $values { "str" "a string" } { "n" "a non-negative integer" } { "ch" "a character"} { "padstr" "a new string" } }
{ $description "Outputs a new string consisting of " { $snippet "ch" } " repeated, that when appended to " { $snippet "str" } ", yields a string of length " { $snippet "n" } ". If the length of { " { $snippet "str" } " is greater than " { $snippet "n" } ", this word outputs the empty string." } ; { $description "Outputs a new string consisting of " { $snippet "ch" } " repeated, that when appended to " { $snippet "str" } ", yields a string of length " { $snippet "n" } ". If the length of { " { $snippet "str" } " is greater than " { $snippet "n" } ", this word outputs the empty string." } ;
HELP: pad-left
{ $values { "str" "a string" } { "n" "a non-negative integer" } { "ch" "a character"} { "padded" "a new string" } }
{ $description "Outputs a new string consisting of " { $snippet "str" } " padded on the left with enough repetitions of " { $snippet "ch" } " to have the result be of length " { $snippet "n" } "." }
{ $examples { $example "{ \"ab\" \"quux\" } [ 5 CHAR: - pad-left print ] each" "---ab\n-quux" } } ;
HELP: pad-right
{ $values { "str" "a string" } { "n" "a non-negative integer" } { "ch" "a character"} { "padded" "a new string" } }
{ $description "Outputs a new string consisting of " { $snippet "str" } " padded on the right with enough repetitions of " { $snippet "ch" } " to have the result be of length " { $snippet "n" } "." }
{ $examples { $example "{ \"ab\" \"quux\" } [ 5 CHAR: - pad-right print ] each" "ab---\nquux-" } } ;
HELP: ch>string HELP: ch>string
{ $values { "ch" "a character"} { "str" "a new string" } } { $values { "ch" "a character"} { "str" "a new string" } }
{ $description "Outputs a string of one character." } ; { $description "Outputs a string of one character." } ;