I am using Tdefs to read data from arrays which are set outside of Tdef’s environment. At the moment the array gets updated when previous one is read to the end. I would like to be able for Tdef to update at each iteration. Array has always the same size, just its content changes. I am not sure how to achieve that and keep the reading position in place. For example if the whole ~array changes when Tdef is at index 2, then the Tdef continues from that index. Here is a version which updates at the end of the loop:
~array = [1,2,3,4,5];
~array = [6,7,8,9,10];
Tdef(\getArray, {
var data = Prout({loop{ ~array.do(_.yield)}}).asStream;
loop{
data.next.postln;
1.wait;
}
})
That’s because ~array.do take the array referenced in ~array and iterate on it. If you replace the array pointed by ~array by another array, the do loop still have the old array reference and don’t see any change.
if you do array[0] = 300; the Prout will see the change since you are modifying the array in place, the array reference stay the same
To take the change in account, you have to evaluate ~array each time to get the last array reference. One way to do it is to iterate on the index:
(
Tdef(\getArray, {
var data = Prout({loop{ ~array.size.do { arg idx;
~array[idx].yield;
}}}).asStream;
loop{
data.next.postln;
1.wait;
}
})
)
You can also iterate on an array that change size by incrementing idx infinitely and test each time if idx is greater than array size then set idx=0 to start again at the begining of the array