discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

nested ternary operators or if statements inside function

DS
Dan Shriver
Wed, Feb 7, 2018 8:52 PM

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;
}
}

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; } }
RP
Ronaldo Persiano
Wed, Feb 7, 2018 9:09 PM

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 tabbydan@gmail.com:

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
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

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 <tabbydan@gmail.com>: > 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 > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org > >
N
NateTG
Wed, Feb 7, 2018 9:11 PM

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/

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/
TP
Torsten Paul
Wed, Feb 7, 2018 9:37 PM

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.

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.