Hello,
I am trying to wrap my head around the Prewrite class.
My understanding is that the three arguments it takes are:
- first key of the dictionary to start with
- IdentityDictionary which defines the rules for rewriting (though Event is used in the
help file examples) - how many iterations of transformations/rewrites to complete
Here is a slightly modified bit of code from the help file I was hoping to use to understand Prewrite’s behavior:
(
a = Prewrite(0, // start with 0
(
0: #[2, 1],
1: #[1, 0],
2: #[0, 2]
), 3);
x = a.asStream;
)
x.next.postln
My expectation is that the dictionary in the second argument uses the keys to specify the rules for transformation, so in this case 0 transforms into the sequence 2, 1. 1 transforms into 1, 0; 2 transforms into 0, 2.
I also expect that the third argument sets how many “complete levels” of transforms to complete before either ending the stream, or looping. And also that the first argument provides the first key to transform/start with.
So in this case here’s what I’d expect to see, hopefully my grouping makes things concise, not distracting or confusing:
// iteration one:
0 // first key, provided as the first argument becomes:
-> [ 2, 1 ] // which then gets fed back in as the first arguments:
// iteration two:
[ 2, 1 ] // results of first iteration used as input
-> [ 0, 2 ] [ 1, 0 ] // which then get fed back in:
// iteration three:
[ 0, 2, 1, 0] // results of second iteration used as input
-> [ 2, 1] [ 0, 2 ], [ 1, 0 ] [ 2, 1 ]
// Prewrite seems to loop, so loop back around and start over
And I almost get that, except when I run the code up top, things seem to wrap back to the start prematurely.
As I evaluate x.next.postln
the results I get is the following chain:
2, 1, 0, 2, 1, 0, 2, 1
, and I’d expect things to keep going and provide 0, 2, 1, 0, 2, 1 next, but
that’s not what happens, at this point the sequence seems to loop back around, providing the next set of numbers: 2, 1, 0, 2, 1, 0, 2, 1
etc… It will keep looping those.
Written another way, again hopefully for clarity, here’s what I get:
// iteration one:
0 // first key becomes:
-> [ 2, 1 ]
// iteration two:
[ 2, 1 ] // results of first iteration used as input
-> [ 0, 2 ] [ 1, 0 ]
// iteration three, here's where the sequence doesn't seem to complete:
[ 0, 2, 1, 0 ] // results of second iteration used as input
-> [ 2, 1 ] // and instead of the expected rest of the sequence, these are the numbers to come out
-> [ 2, 1 ] [ 0, 2 ] [ 1, 0 ] // and repeats and repeats
So ok, perhaps then I’m misunderstanding the iteration levels, instead of three complete input / output cycles, iterations are how many steps from the start position it takes, in this case 3, which would mean it only considers as the inputs (after starting with/translating from the initial key of 0):
2, 1, 0,
Which in this case, it is then understandable the results I get:
2, 1, 0, 2, 1, 0
Each input key returns 2 output numbers, 3 inputs gives me a 6 number long chain. So then I sort of expected a 14 value long chain, so if I set the third argument to 14…
And at this point things get more confusing for me- depending on whether the third argument is an even or odd number, it changes the first returned value, thus the entire sequence differs and all of my assumptions break down.
I’ve looked at the source code and that hasn’t helped me get a better understanding of this behavior.
Can someone please provide some clarity as to how this class actually works and why?
Thanks!
Boris