]> gitweb.factorcode.org Git - factor.git/commitdiff
sequences.extras: rename rotate to rotate-headwards and rotate-tailwards. add experim...
authorDoug Coleman <doug.coleman@gmail.com>
Sat, 2 Apr 2016 22:17:12 +0000 (15:17 -0700)
committerDoug Coleman <doug.coleman@gmail.com>
Sat, 2 Apr 2016 22:17:12 +0000 (15:17 -0700)
extra/sequences/extras/extras-tests.factor
extra/sequences/extras/extras.factor

index 838259fe9ada5bf07003a583ba37bc966430b0b9..6fc8296884a7cfa45fc2103b86b900995e986688 100644 (file)
@@ -46,10 +46,17 @@ 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
+{ t }
+[ "abc" sequence>slice slice? ] unit-test
+
+{ "abc" }
+[ "abc" sequence>slice >string ] unit-test
+
+
+{ "hello" } [ "hello" 0 rotate-headwards ] unit-test
+{ "llohe" } [ "hello" 2 rotate-headwards ] unit-test
+{ "hello" } [ "hello" dup 0 rotate-headwards! ] unit-test
+{ "lohel" } [ "hello" dup 3 rotate-headwards! ] unit-test
 
 { { } } [ { } [ ] map-concat ] unit-test
 { V{ 0 0 1 0 1 2 } } [ 4 iota [ iota ] map-concat ] unit-test
index 811ec9bc4389520f8d1ab1e93f39ee98a7e6bd62..c3e5943c03b2d64107dd8044683f8c7927c4bd97 100644 (file)
@@ -152,10 +152,21 @@ PRIVATE>
 : cut-slice* ( seq n -- before after )
     [ head-slice* ] [ tail-slice* ] 2bi ;
 
-: rotate ( seq n -- seq' )
+: ?<slice> ( from to/f sequence -- slice )
+    over [ nip [ length ] [ ] bi ] unless <slice> ; inline
+
+: sequence>slice ( sequence -- slice )
+    [ drop 0 ] [ length ] [ ] tri <slice> ; inline
+
+: length- ( n sequence -- m ) length swap - ; inline
+
+: rotate-headwards ( seq n -- seq' )
     cut prepend ;
 
-:: rotate! ( seq n -- )
+: rotate-tailwards ( seq n -- seq' )
+    over length- cut prepend ;
+
+:: rotate-headwards! ( seq n -- )
     n seq bounds-check length :> end
     0 n [ 2dup = ] [
         [ seq exchange-unsafe ] [ [ 1 + ] bi@ ] 2bi
@@ -164,7 +175,7 @@ PRIVATE>
     ] until 3drop ;
 
 : all-rotations ( seq -- seq' )
-    dup length iota [ rotate ] with map ;
+    dup length iota [ rotate-headwards ] with map ;
 
 <PRIVATE