Pscratch : FilterPattern

Performs a random walk over a lazy stream of values (unlike Pwalk which takes an eagerly-evaluated list of possible values). This is like scratching a record over an audio stream; hence, Pscratch.

*new(pattern, stepPattern, memorySize = 100)

pattern: The source for the output values.

stepPattern: How many steps forward or backward to move in the stream at each iteration.

memorySize: How many values to retain for backtracking.

p = Pscratch(Pseries(0, 1, inf), (Pwhite(1, 3, inf) * Prand(#[-1, 1], inf)).max(-1)).asStream;

p.nextN(20);

[ 0, 0, 0, 1, 0, 0, 3, 6, 8, 9, 12, 11, 10, 11, 12, 11, 10, 12, 13, 12 ]

Here the initial steps were negative so the first value was repeated.

[ 2, 1, 0, 3, 4, 3, 4, 5, 4, 3, 4, 6, 9, 11, 12, 11, 14, 13, 12, 11 ]

Here the first step was 3, so the third value from pattern was the first to be output. The first two values were available for backtracking.