discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

function with if; case statement;

R
runsun
Fri, Nov 4, 2016 1:18 AM

MichaelAtOz wrote

This is how I lay them out for clarity, with tabs aligning the test, '?'
&':'

function x(p) =
(test)
? true_expression
: false_expression;

function many(p) =
(test1)
?  (test1-t)
?  t_expr
:  f_expr
:  (test1-f)
? t_expr
: f_expr; // etc

Yea, that's a more complete version of what I presented. In summary, for a
construct like: "f = (A)?B:C", next-level branch can be applied to either B
or C or both. My example only shows it applied to C so can be presented
like:

f= (A) ?
B
: (C) ?
C1
: C2

When both branches are nested, your form is a better way:

f= (A)
?  (B)
? B1
: B2
: (C)
? C1
: C2


$  Runsun Pan, PhD $ libs: doctest , faces ( git ), offline doc ( git ), runscad.py ( 2 , git ), synwrite ( 2 );   $ tips: hash ( 2 ), matrix ( 2 , 3 ), sweep ( 2 , 3 ), var ( 2 ), lerp , animation ( gif , prodVid , animlib ), precision ( 2 ), xl-control , type , rounded polygon , chfont , tailRecur ( 2, 3 ), isosphere ( 2 ), area , vol/center , RGB , CurvedImg , tests ( 2 ); $ Apps: rollApp , blockscad , openjscad , on AWS ( pdf )

View this message in context: http://forum.openscad.org/function-with-if-case-statement-tp18882p18926.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

MichaelAtOz wrote > This is how I lay them out for clarity, with tabs aligning the test, '?' > &':' > > function x(p) = > (test) > ? true_expression > : false_expression; > > function many(p) = > (test1) > ? (test1-t) > ? t_expr > : f_expr > : (test1-f) > ? t_expr > : f_expr; // etc Yea, that's a more complete version of what I presented. In summary, for a construct like: "f = (A)?B:C", next-level branch can be applied to either B or C or both. My example only shows it applied to C so can be presented like: > f= (A) ? > B > : (C) ? > C1 > : C2 When both branches are nested, your form is a better way: > f= (A) > ? (B) > ? B1 > : B2 > : (C) > ? C1 > : C2 ----- $ Runsun Pan, PhD $ libs: doctest , faces ( git ), offline doc ( git ), runscad.py ( 2 , git ), synwrite ( 2 );   $ tips: hash ( 2 ), matrix ( 2 , 3 ), sweep ( 2 , 3 ), var ( 2 ), lerp , animation ( gif , prodVid , animlib ), precision ( 2 ), xl-control , type , rounded polygon , chfont , tailRecur ( 2, 3 ), isosphere ( 2 ), area , vol/center , RGB , CurvedImg , tests ( 2 ); $ Apps: rollApp , blockscad , openjscad , on AWS ( pdf ) -- View this message in context: http://forum.openscad.org/function-with-if-case-statement-tp18882p18926.html Sent from the OpenSCAD mailing list archive at Nabble.com.
NH
nop head
Fri, Nov 4, 2016 8:42 AM

The parenthesis are unnecessary because ? : has the lowest operator
precedence.

On 4 November 2016 at 01:18, runsun runsun@gmail.com wrote:

MichaelAtOz wrote

This is how I lay them out for clarity, with tabs aligning the test, '?'
&':'

function x(p) =
(test)
? true_expression
: false_expression;

function many(p) =
(test1)
?  (test1-t)
?  t_expr
:  f_expr
:  (test1-f)
? t_expr
: f_expr; // etc

Yea, that's a more complete version of what I presented. In summary, for a
construct like: "f = (A)?B:C", next-level branch can be applied to either B
or C or both. My example only shows it applied to C so can be presented
like:

f= (A) ?
B
: (C) ?
C1
: C2

When both branches are nested, your form is a better way:

f= (A)
?  (B)
? B1
: B2
: (C)
? C1
: C2


$  Runsun Pan, PhD $ libs: doctest , faces ( git ), offline doc ( git ),
runscad.py ( 2 , git ), synwrite ( 2 );   $ tips: hash ( 2 ), matrix (
2 , 3 ), sweep ( 2 , 3 ), var ( 2 ), lerp , animation ( gif , prodVid ,
animlib ), precision ( 2 ), xl-control , type , rounded polygon , chfont ,
tailRecur ( 2, 3 ), isosphere ( 2 ), area , vol/center , RGB , CurvedImg ,
tests ( 2 ); $ Apps: rollApp , blockscad , openjscad , on AWS ( pdf )

View this message in context: http://forum.openscad.org/
function-with-if-case-statement-tp18882p18926.html
Sent from the OpenSCAD mailing list archive at Nabble.com.


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

The parenthesis are unnecessary because ? : has the lowest operator precedence. On 4 November 2016 at 01:18, runsun <runsun@gmail.com> wrote: > MichaelAtOz wrote > > This is how I lay them out for clarity, with tabs aligning the test, '?' > > &':' > > > > function x(p) = > > (test) > > ? true_expression > > : false_expression; > > > > function many(p) = > > (test1) > > ? (test1-t) > > ? t_expr > > : f_expr > > : (test1-f) > > ? t_expr > > : f_expr; // etc > > Yea, that's a more complete version of what I presented. In summary, for a > construct like: "f = (A)?B:C", next-level branch can be applied to either B > or C or both. My example only shows it applied to C so can be presented > like: > > > f= (A) ? > > B > > : (C) ? > > C1 > > : C2 > > When both branches are nested, your form is a better way: > > > f= (A) > > ? (B) > > ? B1 > > : B2 > > : (C) > > ? C1 > > : C2 > > > > > > ----- > > $ Runsun Pan, PhD $ libs: doctest , faces ( git ), offline doc ( git ), > runscad.py ( 2 , git ), synwrite ( 2 ); &nbsp; $ tips: hash ( 2 ), matrix ( > 2 , 3 ), sweep ( 2 , 3 ), var ( 2 ), lerp , animation ( gif , prodVid , > animlib ), precision ( 2 ), xl-control , type , rounded polygon , chfont , > tailRecur ( 2, 3 ), isosphere ( 2 ), area , vol/center , RGB , CurvedImg , > tests ( 2 ); $ Apps: rollApp , blockscad , openjscad , on AWS ( pdf ) > -- > View this message in context: http://forum.openscad.org/ > function-with-if-case-statement-tp18882p18926.html > Sent from the OpenSCAD mailing list archive at Nabble.com. > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org >
M
MichaelAtOz
Fri, Nov 4, 2016 11:50 AM

Comments are also unnecessary, but I find enclosing the expression is a belt
& braces.
Started when this didn't work.

tn=1;
tb=true;
if tn echo("true tn");
if tb echo("true tb");

Yes the wiki says 'if (expr)' but I can't see why 'if boolean_var' is not
logical.

So, reflex, wrap expressions in '(' & ')'


Admin - PM me if you need anything, or if I've done something stupid...

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.

The TPP is no simple “trade agreement.”  Fight it! http://www.ourfairdeal.org/  time is running out!

View this message in context: http://forum.openscad.org/function-with-if-case-statement-tp18882p18933.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Comments are also unnecessary, but I find enclosing the expression is a belt & braces. Started when this didn't work. tn=1; tb=true; if tn echo("true tn"); if tb echo("true tb"); Yes the wiki says 'if (expr)' but I can't see why 'if boolean_var' is not logical. So, reflex, wrap expressions in '(' & ')' ----- Admin - PM me if you need anything, or if I've done something stupid... Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above. The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out! -- View this message in context: http://forum.openscad.org/function-with-if-case-statement-tp18882p18933.html Sent from the OpenSCAD mailing list archive at Nabble.com.