# Function with logic/cases

6 messages
Open this post in threaded view
|

## Function with logic/cases

 Hi all, is there a (neat) way in openscad to make a function that can use conditions inside? My "mapping" function in c-like pseudocode woud be: function exists(int i) {      if (i < 0) || (i > 161) return false;      if (i > 0 && i < 17) return false;      if (i > 19 && i < 30) return false;      if (i > 37 && i < 48) return false;      if (i > 125 && i < 128) return false;      if (i > 142 && i < 146) return false;      return true } It simply tells true or false, but there are a lot of numbers and the and falses are arranged in groups. It is a mapping of the Mendeleyev periodic table. I'm thinking of producing a vector string in other language and copying it, but I hate the idea. On the other hand, writing down manually 161 cases of ternary operator sounds much worse. Thank you for any advice. Jan _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: Function with logic/cases

 In Pascal I would have defined a hard coded set and then used exists[i]: no function needed at all. Jon On 9/21/2020 5:00 PM, Jan Koupil wrote: > Hi all, > > is there a (neat) way in openscad to make a function that can use > conditions inside? My "mapping" function in c-like pseudocode woud be: > > function exists(int i) { >     if (i < 0) || (i > 161) return false; >     if (i > 0 && i < 17) return false; >     if (i > 19 && i < 30) return false; >     if (i > 37 && i < 48) return false; >     if (i > 125 && i < 128) return false; >     if (i > 142 && i < 146) return false; >     return true > } > > It simply tells true or false, but there are a lot of numbers and the > and falses are arranged in groups. It is a mapping of the Mendeleyev > periodic table. > > I'm thinking of producing a vector string in other language and > copying it, but I hate the idea. On the other hand, writing down > manually 161 cases of ternary operator sounds much worse. > > Thank you for any advice. > > Jan > > _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: Function with logic/cases

Open this post in threaded view
|

## Re: Function with logic/cases

 In reply to this post by janek On 9/21/2020 2:00 PM, Jan Koupil wrote: is there a (neat) way in openscad to make a function that can use conditions inside? My "mapping" function in c-like pseudocode woud be: function exists(int i) {     if (i < 0) || (i > 161) return false;     if (i > 0 && i < 17) return false;     if (i > 19 && i < 30) return false;     if (i > 37 && i < 48) return false;     if (i > 125 && i < 128) return false;     if (i > 142 && i < 146) return false;     return true } It simply tells true or false, but there are a lot of numbers and the and falses are arranged in groups. It is a mapping of the Mendeleyev periodic table. I'm thinking of producing a vector string in other language and copying it, but I hate the idea. On the other hand, writing down manually 161 cases of ternary operator sounds much worse. (I see that while I was typing Adrian covered some of this, but I'm going to leave it here...) I don't really understand the full case, but why isn't the example here just: ```function exists(i) = (i < 0 || i > 161) ? false : (i > 0 && i < 17) ? false : (i > 19 && i < 30) ? false : (i > 37 && i < 48) ? false : (i > 125 && i < 128) ? false : (i > 142 && i < 146) ? false : true; ``` Or, noting that it's false in any of those cases and true otherwise, turn it into a straight logical expression: ```function exists(i) = !( (i < 0 || i > 161) || (i > 0 && i < 17) || (i > 19 && i < 30) || (i > 37 && i < 48) || (i > 125 && i < 128) || (i > 142 && i < 146) ); ``` Or you could get rid of the "not" by applying DeMorgan's laws to invert all of the conditions, describing the condition in terms of the cases where the result is true rather than in terms of the cases where the result is false. _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
 In reply to this post by JordanBrown Here is another (data-driven) approach:valid_ranges = [ [0,0],[17,19],[30,37],[48,125],[128,142],[146,161] ];function exists(i,j=0,found=false) =   j>=len(valid_ranges) || found   ? found   : exists(i,j+1,valid_ranges[j][0]<=i && valid_ranges[j][1]>=i);echo(exists(49)); // trueecho(exists(47)); // falseAlthough the function exists() is less readable, the valid intervals, the only thing to change if needed, are easily readable and maintainable.Em ter., 22 de set. de 2020 às 00:09, Jordan Brown <[hidden email]> escreveu: On 9/21/2020 2:00 PM, Jan Koupil wrote: is there a (neat) way in openscad to make a function that can use conditions inside? My "mapping" function in c-like pseudocode woud be: function exists(int i) {     if (i < 0) || (i > 161) return false;     if (i > 0 && i < 17) return false;     if (i > 19 && i < 30) return false;     if (i > 37 && i < 48) return false;     if (i > 125 && i < 128) return false;     if (i > 142 && i < 146) return false;     return true } It simply tells true or false, but there are a lot of numbers and the and falses are arranged in groups. It is a mapping of the Mendeleyev periodic table. I'm thinking of producing a vector string in other language and copying it, but I hate the idea. On the other hand, writing down manually 161 cases of ternary operator sounds much worse. (I see that while I was typing Adrian covered some of this, but I'm going to leave it here...) I don't really understand the full case, but why isn't the example here just: ```function exists(i) = (i < 0 || i > 161) ? false : (i > 0 && i < 17) ? false : (i > 19 && i < 30) ? false : (i > 37 && i < 48) ? false : (i > 125 && i < 128) ? false : (i > 142 && i < 146) ? false : true; ``` Or, noting that it's false in any of those cases and true otherwise, turn it into a straight logical expression: ```function exists(i) = !( (i < 0 || i > 161) || (i > 0 && i < 17) || (i > 19 && i < 30) || (i > 37 && i < 48) || (i > 125 && i < 128) || (i > 142 && i < 146) ); ``` Or you could get rid of the "not" by applying DeMorgan's laws to invert all of the conditions, describing the condition in terms of the cases where the result is true rather than in terms of the cases where the result is false. _______________________________________________ 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