# nested ternary operators or if statements inside function

 Classic List Threaded
4 messages
Reply | Threaded
Open this post in threaded view
|

## nested ternary operators or if statements inside function

 I am trying to do something in a function one of n ways (in this case one of four ways).First I tried doing a ternary operator nested twice (but it didn't seem to like the syntax) and then I tried if statements.If I have a function and what it does is dependent on a variable with n possible values, is there some way to do it inside that function?  What I did in the past was to use the ternary operator once per helper but this gets fairly tedious (it is easier to read but cumbersome).I have an example where I tried to use "if" statements inside the function (but it didn't work).  Likewise nesting ternary operators inside that function didn't work.  I'd prefer not to create 6 helper functions (two that use the ternary operator to decide which of the four end functions to use)function computeAngle(number, quadrant) =    let(baseAngle = atan(2/(number-1))    if (quadrant == 1) {        baseAngle;    }    else if (quadrant == 2) {        180 - baseAngle;    }    else if (quadrant == 3) {        180 + baseAngle;    }    else {        360 - baseAngle;    }} _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

## Re: nested ternary operators or if statements inside function

 function computeAngle(number, quadrant) =  let( baseAngle = atan(2/(number-1)) )  (quadrant==1) ?      baseAngle   : // else      (quadrant==2) ?          180 - baseAngle      : // else          (quadrant==3) ?              180 + baseAngle          : // else              360 - baseAngle;2018-02-07 18:52 GMT-02:00 Dan Shriver :I am trying to do something in a function one of n ways (in this case one of four ways).First I tried doing a ternary operator nested twice (but it didn't seem to like the syntax) and then I tried if statements.If I have a function and what it does is dependent on a variable with n possible values, is there some way to do it inside that function?  What I did in the past was to use the ternary operator once per helper but this gets fairly tedious (it is easier to read but cumbersome).I have an example where I tried to use "if" statements inside the function (but it didn't work).  Likewise nesting ternary operators inside that function didn't work.  I'd prefer not to create 6 helper functions (two that use the ternary operator to decide which of the four end functions to use)function computeAngle(number, quadrant) =    let(baseAngle = atan(2/(number-1))    if (quadrant == 1) {        baseAngle;    }    else if (quadrant == 2) {        180 - baseAngle;    }    else if (quadrant == 3) {        180 + baseAngle;    }    else {        360 - baseAngle;    }} _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

## Re: nested ternary operators or if statements inside function

 In reply to this post by DanS I've taken to doing something like: (quadrant==1)?    ... :(quadrant==2)?   ... :(quadrant==3)?   ... ://(quadrant=4)   ... ; Which seems to work just fine. -- Sent from: http://forum.openscad.org/_______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

## Re: nested ternary operators or if statements inside function

 If you can also just use quadrant as array index: function computeAngle(number, quadrant) =      let(baseAngle = atan(2/(number-1)))          [              baseAngle,              180 - baseAngle,              180 + baseAngle,              360 - baseAngle          ][quadrant - 1]; ciao,    Torsten. _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org -- Torsten