Hi, Jean-Louis. >From a first glance, the ba.selectn could be faster.
Similarly, another easy-to-read solution that is very similar is using nested ifs: lut(n) = ba.if( n == 2, 3, ba.if( n == 3, 4, ba.if( n == 4, 2, n))); A while back Oleg Nesterov helped me write a function with pattern matching to implement nested ifs with arbitrary if0-then0-if1-then1...ifN-1-thenN-1-else: ifN((c, t, e)) = ba.if(c, t, ifN(e)); ifN(e) = e; you could then write your lut function as follows: lut(n) = ifN((n == 2, 3, n == 3, 4, n == 4, 2, n)); Ciao, Dr Dario Sanfilippo http://dariosanfilippo.com On Thu, 13 Jan 2022 at 19:08, Jean-Louis Paquelin <jean-lo...@paquelin.net> wrote: > @James and @bart, thank you for your quick reply. > > So (if I understand you well) pattern matching should be thought of as a > static rewriting tool. > And, btw, you're right > > process = 2 : lut : hbargraph("B", 1, 5); > > sets the bargraph to 2, while > > process = lut(2) : hbargraph("B", 1, 5); > > sets it to 3 (as expected). > > I came with 2 ways to write the lut() function I need: > > lut1(n) = 1,3,4,2,5 : ba.selectn(5, n - 1); > lut2(n) = ((n == 1) | (n == 5)) * n + (n == 2) * 3 + (n == 3) * 4 + (n == > 4) * 2; > process = hslider("A", 1, 1, 5, 1) <: (lut1 : hbargraph("B", 1, 5)), (lut2 > : hbargraph("C", 1, 5)); > > Both functions seem to provide the service I'm asking for. But the first > one is more readable than the second. > Any idea which one is the lightest for the CPU? > > Thanks again, > > jlp > > Le jeu. 13 janv. 2022 à 16:41, James Mckernon <jmcker...@gmail.com> a > écrit : > >> 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 >
_______________________________________________ Faudiostream-users mailing list Faudiostream-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/faudiostream-users