sequences.extras: make rotate! support negative indices.

locals-and-roots
John Benediktsson 2016-04-04 11:13:11 -07:00
parent 4b5b058c20
commit 4c6511a058
2 changed files with 6 additions and 5 deletions

View File

@ -79,7 +79,7 @@ IN: sequences.extras.tests
{ "hello" } [ "hello" dup 0 rotate! ] unit-test
{ "lohel" } [ "hello" dup 3 rotate! ] unit-test
[ "hello" dup -1 rotate! ] must-fail
{ "ohell" } [ "hello" dup -1 rotate! ] unit-test
{ { } } [ { } [ ] map-concat ] unit-test
{ V{ 0 0 1 0 1 2 } } [ 4 iota [ iota ] map-concat ] unit-test

View File

@ -196,13 +196,14 @@ ERROR: slices-don't-touch slice1 slice2 ;
] if ;
: rotate ( seq n -- seq' )
dup 0 > [ cut ] [ abs cut* ] if prepend ;
dup 0 >= [ cut ] [ abs cut* ] if prepend ;
:: rotate! ( seq n -- )
n seq bounds-check length :> end
0 n [ 2dup = ] [
seq length :> len
n dup 0 < [ len + ] when seq bounds-check drop 0 over
[ 2dup = ] [
[ seq exchange-unsafe ] [ [ 1 + ] bi@ ] 2bi
dup end = [ drop over ] when
dup len = [ drop over ] when
2over = [ -rot nip over ] when
] until 3drop ;