hey is there a pattern function for selecting a value from a list with the same index used for another pattern
for example
Pxrand([ 3,2,4,5],inf) and Pxrand([30,20,40,50],inf)
that selects for both the same index 2 and 20 or 5 and 50 in the same event
hope this make sense, thanks !!
perhaps my structure can be changed to remove the need for it
dkmayer
February 14, 2021, 3:00pm
3
There are many ways to do this, e.g.:
Pbind(
\x, Pindex((2..5), Pxrand((0..3), inf)),
\y, Pkey(\x) * 10
).asStream.nextN(10, ())
Pbind(
\x, Pxrand((0..3), inf).collect { |i| (2..5)[i] },
\y, Pkey(\x) * 10
).asStream.nextN(10, ())
// same with partial application syntax
Pbind(
\x, Pxrand((0..3), inf).collect((2..5)[_]),
\y, Pkey(\x) * 10
).asStream.nextN(10, ())
Pbind(
\a, Pxrand((2..5), inf),
\x, Pkey(\a),
\y, Pkey(\a) * 10
).iter.nextN(10, ())
Pbind(
\a, Pxrand([(2..5), (2..5) * 10].flop, inf),
\x, Pkey(\a).collect(_[0]),
\y, Pkey(\a).collect(_[1])
).asStream.nextN(10, ())
Note that this construction in a Pbind can be simplified:
[\x, \y], Pkey(\a)
… which, then, in this example means that \a
and the Pkey could be dropped, though that may not apply to other situations.
One approach that I use often, but which isn’t obvious, is to calculate an array index under one key and then use Pindex or Pswitch1 with different arrays:
\index, Pwhite(0, oneArray.size - 1),
\x, Pindex(oneArray, Pkey(\index)),
\y, Pindex(otherArray, Pkey(\index)),
hjh
1 Like
i was wondering if a key from pbind (using pkey) could be sent as another argument , thanks heaps !! I put it all together now