circular-while, fix of by 1 error; add tests
parent
b5700cd3f5
commit
3a48ab4688
|
@ -43,6 +43,26 @@ IN: circular.tests
|
||||||
] with each >array
|
] with each >array
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
|
[ V{ 1 2 3 } ] [
|
||||||
|
{ 1 2 3 } <circular> V{ } [
|
||||||
|
[ push f ] curry circular-while
|
||||||
|
] keep
|
||||||
|
] unit-test
|
||||||
|
|
||||||
|
CONSTANT: test-sequence1 { t f f f }
|
||||||
|
[ V{ 1 2 3 1 } ] [
|
||||||
|
{ 1 2 3 } <circular> V{ } [
|
||||||
|
[ [ push ] [ length 1 - test-sequence1 nth ] bi ] curry circular-while
|
||||||
|
] keep
|
||||||
|
] unit-test
|
||||||
|
|
||||||
|
CONSTANT: test-sequence2 { t f t t f f t t t f f f }
|
||||||
|
[ V{ 1 2 3 1 2 3 1 2 3 1 2 3 } ] [
|
||||||
|
{ 1 2 3 } <circular> V{ } [
|
||||||
|
[ [ push ] [ length 1 - test-sequence2 nth ] bi ] curry circular-while
|
||||||
|
] keep
|
||||||
|
] unit-test
|
||||||
|
|
||||||
[ V{ 1 2 3 1 2 } ] [
|
[ V{ 1 2 3 1 2 } ] [
|
||||||
{ 1 2 3 } <circular> V{ } [
|
{ 1 2 3 } <circular> V{ } [
|
||||||
[ [ push ] [ length 5 < ] bi ] curry circular-while*
|
[ [ push ] [ length 5 < ] bi ] curry circular-while*
|
||||||
|
|
|
@ -60,14 +60,14 @@ TUPLE: circular-iterator
|
||||||
{ circular read-only } { n integer } { last-start integer } ;
|
{ circular read-only } { n integer } { last-start integer } ;
|
||||||
|
|
||||||
: <circular-iterator> ( circular -- obj )
|
: <circular-iterator> ( circular -- obj )
|
||||||
0 0 circular-iterator boa ; inline
|
0 -1 circular-iterator boa ; inline
|
||||||
|
|
||||||
<PRIVATE
|
<PRIVATE
|
||||||
|
|
||||||
: (circular-while) ( ... iterator quot: ( ... obj -- ... ? ) -- ... )
|
: (circular-while) ( ... iterator quot: ( ... obj -- ... ? ) -- ... )
|
||||||
[ [ [ n>> ] [ circular>> ] bi nth ] dip call ] 2keep
|
[ [ [ n>> ] [ circular>> ] bi nth ] dip call ] 2keep
|
||||||
rot [ [ dup n>> >>last-start ] dip ] when
|
rot [ [ dup n>> >>last-start ] dip ] when
|
||||||
over [ n>> ] [ [ last-start>> ] [ circular>> length ] bi + 1 - ] bi = [
|
over [ n>> ] [ [ last-start>> ] [ circular>> length ] bi + ] bi = [
|
||||||
2drop
|
2drop
|
||||||
] [
|
] [
|
||||||
[ [ 1 + ] change-n ] dip (circular-while)
|
[ [ 1 + ] change-n ] dip (circular-while)
|
||||||
|
|
Loading…
Reference in New Issue