Sharing: Convert standard if/then to jit.expr

    Feb 13 2012 | 3:15 am
    After two days of finally figuring out the basics of if/then statements using jit.expr (thanks Dieter Vandoren), I figured there must be a way to parse more traditional structures into what jit.expr is expecting.
    Voila - below is an abstraction that takes Max's standard format for the "if" object and converts it. Still not perfect, but might be helpful to those wanting a quick way to do comparative statements in Jitter.
    For example:if $i1 >= $i2 && $i1
    ... converts to:(in[0]>=in[1])*(in[0]
    Suggestions or bugginess welcomed!

    • Feb 13 2012 | 6:27 am
      I don't think you need the delay in this. The bang won't be sent from trigger until the entire list has been outputted by iter.
    • Feb 13 2012 | 10:49 am
    • Feb 13 2012 | 1:33 pm
      @David - perhaps not. I wanted to be sure the regexp had time to do it's thing and not prematurely truncate the string, so the delay is a safety net.
    • Feb 13 2012 | 4:40 pm
      Unless regexp delays its operation to another event queue (which I presume it doesn't do), Max won't move on to other events until it's finished dealing with the entire branch of events leading from the list output of trigger.
      Useful little tool though!
    • Feb 14 2012 | 4:25 am
      Brilliant! I was just about to post with question and this tool solved it! Thanks so much
    • Feb 14 2012 | 12:58 pm
      jit.expr groks the standard C ternary if-then-else operator
      So $i1 >= $i2 && $i1
      becomes ($i1 >= $i2 && $i1
      You can also use the equivalence between arithmetic multiplication and Boolean AND to rewrite as
      ($i1 >= $i2)*($i1 , but that is a very C-geekish formulation.
      Note that since these are expressions, they must always evaluate to something, even if it is zero. This is in contrast to an if-statement without an else clause in a procedural language (which is what OP asked about).
    • Feb 14 2012 | 2:11 pm
      Hi Peter, are you saying that '?' can serve as a 'then' operator and ':' as an 'else'? I tried it in jit.expr without any luck (though it doesn't break it either).