Sharing: Convert standard if/then to jit.expr

Jeff Thompson's icon

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]

Max Patch
Copy patch and select New From Clipboard in Max.

Suggestions or bugginess welcomed!

David Butler's icon

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.

dtr's icon

nice!

Jeff Thompson's icon

@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.

David Butler's icon

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!

berko's icon

Brilliant! I was just about to post with question and this tool solved it! Thanks so much

Peter Castine's icon

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).

Jeff Thompson's icon

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).