]> gitweb.factorcode.org Git - factor.git/commitdiff
sequences.extras: make rotate! support negative indices.
authorJohn Benediktsson <mrjbq7@gmail.com>
Mon, 4 Apr 2016 18:13:11 +0000 (11:13 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Mon, 4 Apr 2016 18:13:11 +0000 (11:13 -0700)
extra/sequences/extras/extras-tests.factor
extra/sequences/extras/extras.factor

index dfdc2837f9ea928742bcb85825741b7784d13d6f..c1de93f07610694af626e703270aa7db6112e2ac 100644 (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
index b37ca03bd568e18575a4923dc3941256e06a28c5..ec9b5c0bb53e2d7cf12b025b11ff5ce718fd90fe 100644 (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 ;