From: John Benediktsson Date: Tue, 29 May 2012 18:59:03 +0000 (-0700) Subject: sequences: change prepend to return type of first sequence to match append. X-Git-Tag: 0.97~3054 X-Git-Url: https://gitweb.factorcode.org/gitweb.cgi?p=factor.git;a=commitdiff_plain;h=1a6be98c45bba6e0f589e6ee677938f7a55877eb sequences: change prepend to return type of first sequence to match append. --- diff --git a/core/sequences/sequences-docs.factor b/core/sequences/sequences-docs.factor index f3dfebedf7..9e3c197a9d 100644 --- a/core/sequences/sequences-docs.factor +++ b/core/sequences/sequences-docs.factor @@ -785,11 +785,11 @@ HELP: append-as HELP: prepend { $values { "seq1" sequence } { "seq2" sequence } { "newseq" sequence } } -{ $description "Outputs a new sequence of the same type as " { $snippet "seq2" } " consisting of the elements of " { $snippet "seq2" } " followed by " { $snippet "seq1" } "." } -{ $errors "Throws an error if " { $snippet "seq1" } " contains elements not permitted in sequences of the same class as " { $snippet "seq2" } "." } +{ $description "Outputs a new sequence of the same type as " { $snippet "seq1" } " consisting of the elements of " { $snippet "seq2" } " followed by " { $snippet "seq1" } "." } +{ $errors "Throws an error if " { $snippet "seq2" } " contains elements not permitted in sequences of the same class as " { $snippet "seq1" } "." } { $examples { $example "USING: prettyprint sequences ;" "{ 1 2 } B{ 3 4 } prepend ." - "B{ 3 4 1 2 }" + "{ 3 4 1 2 }" } { $example "USING: prettyprint sequences strings ;" "\"go\" \"car\" prepend ." @@ -797,6 +797,23 @@ HELP: prepend } } ; +HELP: prepend-as +{ $values { "seq1" sequence } { "seq2" sequence } { "exemplar" sequence } { "newseq" sequence } } +{ $description "Outputs a new sequence of the same type as " { $snippet "exemplar" } " consisting of the elements of " { $snippet "seq2" } " followed by " { $snippet "seq1" } "." } +{ $errors "Throws an error if " { $snippet "seq1" } " or " { $snippet "seq2" } " contain elements not permitted in sequences of the same class as " { $snippet "exemplar" } "." } +{ $examples + { $example "USING: prettyprint sequences ;" + "{ 3 4 } B{ 1 2 } B{ } prepend-as ." + "B{ 1 2 3 4 }" + } + { $example "USING: prettyprint sequences strings ;" + "\"ing\" \"go\" SBUF\" \" prepend-as ." + "SBUF\" going\"" + } +} ; + +{ prepend prepend-as } related-words + HELP: 3append { $values { "seq1" sequence } { "seq2" sequence } { "seq3" sequence } { "newseq" sequence } } { $description "Outputs a new sequence consisting of the elements of " { $snippet "seq1" } ", " { $snippet "seq2" } " and " { $snippet "seq3" } " in turn." } diff --git a/core/sequences/sequences-tests.factor b/core/sequences/sequences-tests.factor index 86624c0c19..9d560facbd 100644 --- a/core/sequences/sequences-tests.factor +++ b/core/sequences/sequences-tests.factor @@ -1,6 +1,6 @@ -USING: arrays kernel math math.order math.parser namespaces -sequences kernel.private sequences.private strings sbufs -tools.test vectors assocs generic vocabs.loader ; +USING: arrays byte-arrays kernel math math.order math.parser +namespaces sequences kernel.private sequences.private strings +sbufs tools.test vectors assocs generic vocabs.loader ; IN: sequences.tests [ "empty" ] [ { } [ "empty" ] [ "not empty" ] if-empty ] unit-test @@ -109,6 +109,9 @@ unit-test [ "a" -1 append ] must-fail [ -1 "a" append ] must-fail +{ t } [ B{ 0 } { 1 } append byte-array? ] unit-test +{ t } [ B{ 0 } { 1 } prepend byte-array? ] unit-test + [ [ ] ] [ 1 [ ] remove ] unit-test [ [ ] ] [ 1 [ 1 ] remove ] unit-test [ [ 3 1 1 ] ] [ 2 [ 3 2 1 2 1 ] remove ] unit-test diff --git a/core/sequences/sequences.factor b/core/sequences/sequences.factor index 0f208e2cab..33f2924769 100644 --- a/core/sequences/sequences.factor +++ b/core/sequences/sequences.factor @@ -352,7 +352,9 @@ PRIVATE> : append ( seq1 seq2 -- newseq ) over append-as ; -: prepend ( seq1 seq2 -- newseq ) swap append ; inline +: prepend-as ( seq1 seq2 exemplar -- newseq ) swapd append-as ; inline + +: prepend ( seq1 seq2 -- newseq ) over prepend-as ; inline : 3append ( seq1 seq2 seq3 -- newseq ) pick 3append-as ;