On 1/13/22, Jean-Louis Paquelin <jean-lo...@paquelin.net> wrote:
> Hello there,
>
> I can't figure out how the pattern matching works in Faust.
>
> I've tried the following code:
>
> lut(2) = 3;
> lut(3) = 4;
> lut(4) = 2;
> lut(n) = n;
>
> process = hslider("A", 1, 1, 5, 1) : int : lut : hbargraph("B", 1, 5);
>
> Trying this in the IDE, the B bargraph simply replicates the A slider
> value. I was expecting a different behavior thru the lut function: the
> output of lut should be the same as its input except when the input is 2, 3
> or 4, in those cases the output of lut should be 3, 4, 2 respectively.
>
> I tried the alternative syntax without any success:
>
> lut = case {
> (2) => 3;
> (3) => 4;
> (4) => 2;
> (n) => n;
> };
>
> It's as if none of the cases match except the most general one.
>
> Did I miss something?
>
> I wish you all the best for 2022, health, joy and great projects!
>
> Best regards,
>
> Jean-Louis

Pattern matching is done at compile-time, so to match the case of
lut(2) (for example), the argument needs to be fixed and known at
compile-time to be 2. This is true of lut(2), even of expressions such
as lut(1 + 1), but not taking lut as a function of some potentially
time-varying expression.

Also, I'm not 100% sure of this, but I think pattern-matching may only
take effect when a function is applied with explicit, parenthesis
syntax, not when it is chained with : (i.e. lut (x) rather than x :
lut). If I'm wrong about this then perhaps someone can correct me; in
any case I don't think this was the problem with your example.

The way to do what you are attempting to do at run-time would probably
be by chaining a number of select2 functions together. I'm not sure
whether there are any helper functions in the standard library to help
with this; it seems like the sort of thing that ought to be present,
but I can't think of any off the top of my head.

Hope that helps.


_______________________________________________
Faudiostream-users mailing list
Faudiostream-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/faudiostream-users

Reply via email to