sequences.extras: adding rotate and rotate! words.

db4
John Benediktsson 2012-04-24 16:59:17 -07:00
parent f24e40800a
commit 476781cdd9
2 changed files with 16 additions and 0 deletions

View File

@ -49,3 +49,8 @@ IN: sequences.extras.tests
{ { "hello," " " "world!" " " " " } }
[ "hello, world! " [ blank? ] slice-when [ >string ] map ] unit-test
{ "hello" } [ "hello" 0 rotate ] unit-test
{ "llohe" } [ "hello" 2 rotate ] unit-test
{ "hello" } [ "hello" dup 0 rotate! ] unit-test
{ "lohel" } [ "hello" dup 3 rotate! ] unit-test

View File

@ -103,3 +103,14 @@ IN: sequences.extras
[ 2dup = [ 1 + ] when ] [ len ] if*
[ seq <slice> ] keep len or swap
] produce nip ; inline
: rotate ( seq n -- seq' )
cut prepend ;
:: rotate! ( seq n -- )
seq length :> end
n 0 n [ 2dup = ] [
[ seq exchange ] [ [ 1 + ] bi@ ] 2bi
dup end = [ drop over ] when
2over = [ -rot nip over ] when
] until 3drop ;