Forums > Jitter

Sharing: Convert standard if/then to jit.expr

February 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 < = $i3 then $i4

... converts to:
(in[0]>=in[1])*(in[0]< =in[2])*in[3]

Suggestions or bugginess welcomed!


----------begin_max5_patcher----------
2485.3ocya01aaajD9y1+JlSnMmbsrJW9pXZZPLNXbHE85UjjOT.aiBJwURa
BEoJWRKqzz+6clcWZS6JGRCKxj.Dwr7kcm8gOyLOyxM+4gGLXZ10b4.34v4v
AG7mGdvApSQm3.S6CFrJ55YIQR0sMXV1pU7zhAizWqfecg57u.lxwqwkPwRg
7eUc804bId6QEhrzeOmOqPOV1VSFaMBbBBnCVlefKMOkHV0mYSe+ILa+p9Js
bkHMgWnLDl4jyyRKRiVwUOvo4hnjZ2dVYQ08aUYPQEyVJRWTyXbrTFfiu5vD
a5W1jZlCMFRwGUiACuI5r+0gGR+LpkvVJeCNa9Gn16EEi4WuNGdkHccYgDbg
WoZOTjdt0ku7GwCrKO56zMeA0zFahGbtbvt.qfcBVtORvx9yAV0vJa++IXo6
lhsq45Gfli+9pnhbw0CFACFrOQ0GlLRjvwiGuSLxqqIT1LMHowJOVORnDygu
QvfW9i3Aa3YOS05ETKGDS3o3+vcmfhydg3vZj3nOv7cZl3zKTEbHQfIOqbwR
.AOb5LkmCBIjvkTrrnTbDgrbXQNOp.uj4TVU8ThHEC7UlVT2w4NPqUWy2Bln
.UsOIywU4ZFte4aXjcYzB9CQ3dAgRe5SZxGAOUrMVCYBBb7UVsUyYBX6NSf8
9iiZ.Rlkm5fq9f+WbR5Y+1o+ue8mO6sOeWttrNOdlqFOztt9S5hvYOzLeylM
ieOe97b91hkYqVKyRGmkuXm3fS2KTfl5t1ZRhsWeB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ULyVti3t118z5I
Uolz2+wHmTcOpEE6d+uZPMPz4uKvJyJymU8V5lcYJb6XESampTEcr1cQEvU6
lVJhi4o0mWqDwpsjkrZikFp9HmN5oFy8lVvk6jAzVykV7llsVmGm0ZrOWzYy
azcZ8DsV0V9XuCtdgtcD3FzBqkRO9XrVeOk84nSyYrVUqmL3xZg4du4Til6S
EBIpGqAa59VdyXXfOgZ1ASnCFDU05IZt26k4C.gS5UHT4NXumYgdNJeaalpJ
DWSYT2o09mgRqI+d2ept8sWs16EF5A3Bg8OWnI2o.md0lB5hX56ivNN6YlV.
pHoSXZTsrfaSLsd9sJqEHnOq+solhDRqgWeZSNsIl1W.bpQa5Q5Q5Fnyxp2w
m0asOr1lho4+Hiy5wBqYs0a8Ti.61A4v776pbXcQFWCZ1AwAs5B8AAcE1x5h
pCrZE1hM9qC+aw4QYcC
-----------end_max5_patcher-----------

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



dtr
February 13, 2012 | 10:49 am

nice!


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


February 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!


February 14, 2012 | 4:25 am

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


February 14, 2012 | 12:58 pm

jit.expr groks the standard C ternary if-then-else operator

So $i1 >= $i2 && $i1 < = $i3 then $i4 else 0

becomes ($i1 >= $i2 && $i1 < = $i3) ? $i4 : 0

You can also use the equivalence between arithmetic multiplication and Boolean
AND to rewrite as

($i1 >= $i2)*($i1 < = $i3)*$i4, 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).


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


Viewing 8 posts - 1 through 8 (of 8 total)