pad-left/right now work with any sequence
parent
4ed790c2b6
commit
8a0d2e90d9
|
|
@ -36,7 +36,7 @@ $terpri
|
|||
|
||||
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:"
|
||||
{ $example "5 dup ." "25" }
|
||||
{ $example "5 dup * ." "25" }
|
||||
"The " { $link sq } " word is actually defined as follows:"
|
||||
{ $code ": sq dup * ;" }
|
||||
"(You could have looked this up yourself by clicking on the " { $link sq } " word itself.)"
|
||||
|
|
|
|||
|
|
@ -124,6 +124,15 @@ M: object like drop ;
|
|||
|
||||
: 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 -- ? )
|
||||
2dup [ length ] 2apply tuck number=
|
||||
[ (mismatch) -1 number= ] [ 3drop f ] if ; inline
|
||||
|
|
|
|||
|
|
@ -196,6 +196,16 @@ HELP: last/first
|
|||
{ $values { "seq" "a sequence" } { "pair" "a two-element array" } }
|
||||
{ $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=
|
||||
{ $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." } ;
|
||||
|
|
|
|||
|
|
@ -48,15 +48,6 @@ UNION: alpha Letter digit ;
|
|||
: >lower ( str -- lower ) [ ch>lower ] 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> ;
|
||||
|
||||
: >string ( seq -- str )
|
||||
|
|
|
|||
|
|
@ -66,16 +66,6 @@ HELP: padding
|
|||
{ $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." } ;
|
||||
|
||||
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
|
||||
{ $values { "ch" "a character"} { "str" "a new string" } }
|
||||
{ $description "Outputs a string of one character." } ;
|
||||
|
|
|
|||
Loading…
Reference in New Issue