From 8a0d2e90d9abeb2f629daff7f322773a38cfa1ed Mon Sep 17 00:00:00 2001 From: slava Date: Thu, 7 Sep 2006 21:14:08 +0000 Subject: [PATCH] pad-left/right now work with any sequence --- doc/handbook/cookbook.facts | 2 +- library/collections/sequences-epilogue.factor | 9 +++++++++ library/collections/sequences-epilogue.facts | 10 ++++++++++ library/collections/strings.factor | 9 --------- library/collections/strings.facts | 10 ---------- 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/doc/handbook/cookbook.facts b/doc/handbook/cookbook.facts index b0a4c6836e..87e4264036 100644 --- a/doc/handbook/cookbook.facts +++ b/doc/handbook/cookbook.facts @@ -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.)" diff --git a/library/collections/sequences-epilogue.factor b/library/collections/sequences-epilogue.factor index e09963e74f..a88a47f563 100644 --- a/library/collections/sequences-epilogue.factor +++ b/library/collections/sequences-epilogue.factor @@ -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> ; + +: 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 diff --git a/library/collections/sequences-epilogue.facts b/library/collections/sequences-epilogue.facts index c18236cae9..5bc55a5a10 100644 --- a/library/collections/sequences-epilogue.facts +++ b/library/collections/sequences-epilogue.facts @@ -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." } ; diff --git a/library/collections/strings.factor b/library/collections/strings.factor index 7fc2552e2f..2843e6cbd2 100644 --- a/library/collections/strings.factor +++ b/library/collections/strings.factor @@ -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> ; - -: 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 ( seq -- str ) diff --git a/library/collections/strings.facts b/library/collections/strings.facts index 1351cd6d95..2e77e0ebf6 100644 --- a/library/collections/strings.facts +++ b/library/collections/strings.facts @@ -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." } ;