Hello everyone,
I’m trying to find a pattern that will do a random walk over a list, and when it reaches a boundary, it always folds back towards the center. I expected Pwalk
could easily achieve this behavior, but I can’t figure out how to do it.
The help file for Pwalk
says to use directionPattern = 1
to wrap, so I would expect -1 to fold, but I get unexpected results with the most simple use cases. Here I try both directions, and neither one works as expected on both boundaries:
Pwalk([5.1, 6, 7], Prand([-1, 1], 30), -1).iter.all;
// ...folds at upper bound, but wraps at lower bound
Pwalk([5.1, 6, 7], Prand([-1, 1], 30), 1).iter.all;
// ...folds at lower bound, but wraps at upper bound
It seems that Pwalk
needs to treat each boundary differently in order to achieve my desired result, but after looking at the source code, I don’t think Pwalk
can do this. Please correct me if I’m wrong.
I have come up with this workaround using Pbrown
, but it has its own limitations:
Pbrown(0, 2, 1).collect([5.1, 6, 7].at(_)).iter.nextN(20);
That works fine for this simple case, but Pbrown
is limited in that it can’t do more complicated (i.e. asymmetrical) step patterns, and you can’t embed Pbinds in the resulting stream, as far as I can tell.
So my question is, is it possible to achieve this folded random walk pattern with Pwalk
, and if so, what is the directionPattern
that does this? Or is there some other pattern class that I should be using? I know I could use Prout
and write all the logic myself, but if that’s the case I’d prefer to write a new pattern class.