Please explain Pfunc for me

Hi all

Although reading the docs and some forum posts, Pfunc still remains partly inscrutable to me.

Let’s take some stuff from the official documentation.
There’s written:

nextFunc Stream function. In an event stream nextFunc receives the current Event as argument, and more generally the argument passed to the stream’s next call.

How do I have to understand “current Event”? What’s that in the end?

Next snippet:

x = Pfunc({ |inval| (10 ** inval.value) * rrand(1, 9) }).asStream;
[2, 3, 2].do { |i| };
x.nextN(5, (:0..)).postln;

I can’t make much sense of “inval”. What kind of values does this actually produce and where do they come from?
What does “(:0…)” stand for?

Thanks a lot for clarification!

A Pfunc is a Pattern construct of a function.
The inval is the event received by the Pfunc that would be used by the Pfunc to make its work.

Traditionally it is used in aPbind.

Pbind(\instrument, \shh,
        \dur, Pwhite(2,5,1), // 1 répétition
		\amp, 0.05,
		\len, Pfunc({|evt| evt[\dur]-1;

In this example, the Pfunc receives the event generated by the Pbind (evt) to compute a new field (\len) based on the event’s \dur field.

In your example, you can see your Pfunc as a function used repeatability and producing a new value at each call of its nextmethod.

In :

[2, 3, 2].do { |i| };

The inval receives by the Pfunc are 2, then 3, then 2.

In :

x.nextN(5, (:0..)).postln;

The inval receives by the Pfunc is produced by the Routine (:0..) that produces numbers form 0 at call of its own next method.

Just try this, to see this Routine in action:

1 Like

Ok, thanks for your explanation!

I can make sense out of the Pbind thing, what the inval is and (:0…) now.

What kind of confuses me:
If I evaluate the Pfunc example I might receive the following values:

[ 5.0, 70.0, 800.0, 2000.0, 90000.0 ]

I can make sense of the numbers “800.0, 1000.0, 700.0” and that “x.nextN(5, (:0…))” will produce 5 numbers, but how exactly these 5?
Can’t quite see how it’s connected.

but how exactly these 5?

Your Pfunc is called 5 times. And it gets its “inval” values from a Routine iterating numbers starting a 0. So each time the Pfunc is executed it is asking to the Routine “Give me your current value”. And each time the routine is asked for its value it gives the value incremented by 1. And this 5 times.

Just try this:

x.value; // outputs 0
x.value; // outputs 1
x.value; // outputs 2
x.value; // outputs 3

I’ll suggest you to remove the random part of the Pfunc to see more clearly the link between the Array (instruction (2)), the Routine (instruction (3)) and the output of the Pfunc.

hmmm… I think I got it now.
So “x.nextN(5, (:0…))” is used again as a new “inval”…
Couldn’t see through this before.

Thanks for your time!

1 Like