sequences.extras: adding rotate and rotate! words.
parent
f24e40800a
commit
476781cdd9
|
@ -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
|
||||
|
|
|
@ -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 ;
|
||||
|
|
Loading…
Reference in New Issue