sequences.repeating: rename repeating to cycle and add repeat

db4
John Benediktsson 2012-07-10 16:47:33 -07:00
parent 1a9caec368
commit f932ca2090
3 changed files with 32 additions and 14 deletions

View File

@ -1,5 +1,8 @@
USING: sequences.repeating tools.test ;
IN: sequences.repeating.tests
[ { 1 2 3 1 2 } ] [ { 1 2 3 } 5 repeated ] unit-test
[ { 1 2 3 1 2 3 1 2 3 } ] [ { 1 2 3 } 9 repeated ] unit-test
[ { 1 2 3 1 2 } ] [ { 1 2 3 } 5 cycle ] unit-test
[ { 1 2 3 1 2 3 1 2 3 } ] [ { 1 2 3 } 9 cycle ] unit-test
[ { } ] [ { 1 2 3 } 0 repeat ] unit-test
[ { 1 1 1 2 2 2 3 3 3 } ] [ { 1 2 3 } 3 repeat ] unit-test

View File

@ -1,21 +1,36 @@
! Copyright (C) 2008 Alex Chapman
! Copyright (C) 2012 John Benediktsson
! See http;//factorcode.org/license.txt for BSD license
USING: accessors circular kernel sequences ;
USING: accessors circular kernel math sequences sequences.private ;
IN: sequences.repeating
TUPLE: repeating circular len ;
TUPLE: cycles circular length ;
: <repeating> ( seq length -- repeating )
[ <circular> ] dip repeating boa ;
: <cycles> ( seq length -- cycles )
[ <circular> ] dip cycles boa ;
: repeated ( seq length -- new-seq )
dupd <repeating> swap like ;
: cycle ( seq length -- new-seq )
dupd <cycles> swap like ;
M: repeating length len>> ;
M: repeating set-length len<< ;
M: cycles length length>> ;
M: cycles set-length length<< ;
M: repeating virtual@ ( n seq -- n' seq' ) circular>> ;
M: cycles virtual@ ( n seq -- n' seq' ) circular>> ;
M: repeating virtual-exemplar circular>> ;
M: cycles virtual-exemplar circular>> ;
INSTANCE: repeating virtual-sequence
INSTANCE: cycles virtual-sequence
TUPLE: repeats seq length ;
: <repeats> ( seq times -- repeats )
[ dup length ] dip * repeats boa ;
: repeat ( seq times -- new-seq )
dupd <repeats> swap like ;
M: repeats length length>> ;
M: repeats nth-unsafe seq>> [ length /i ] keep nth ;
INSTANCE: repeats immutable-sequence

View File

@ -7,7 +7,7 @@ MEMO: single-sine-wave ( samples/wave -- seq )
[ iota ] [ pi 2 * swap / [ * sin ] curry ] bi map ;
: (sine-wave) ( samples/wave n-samples -- seq )
[ single-sine-wave ] dip <repeating> ;
[ single-sine-wave ] dip <cycles> ;
: sine-wave ( sample-freq freq seconds -- seq )
pick * >integer [ /i ] dip (sine-wave) ;