]> gitweb.factorcode.org Git - factor.git/commitdiff
sequences.extras: adding rotate and rotate! words.
authorJohn Benediktsson <mrjbq7@gmail.com>
Tue, 24 Apr 2012 23:59:17 +0000 (16:59 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 24 Apr 2012 23:59:17 +0000 (16:59 -0700)
extra/sequences/extras/extras-tests.factor
extra/sequences/extras/extras.factor

index 968f827997a12d6b7f7bf74ef26d38c87f5d40d1..ec7d9ec17fea597f167c9f6f298181a5a74071d0 100644 (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
index eac2f5236aa6a185f5bbcce450199cb7183c8f26..323a4c076ffe5f51c91757a051af20faf496fd45 100644 (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 ;