Lookahead in a Stream (without "consuming" its next element?)

For duplicating Patterns you can check PSdup from miSCellaneous_lib.

But actually, as James mentioned, you need buffering primarily. PSx pattern also have a buffering option.

p = PS(Pwhite(0, 100), inf, 10);
q = p.iter

q.nextN(10)

-> [ 17, 67, 28, 17, 31, 86, 5, 11, 35, 80 ]

p.lastValues

-> [ 80, 35, 11, 5, 86, 31, 17, 28, 67, 17 ]

p[1]

-> 35

This also works for event patterns. Hope this helps.

2 Likes