discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Function with logic/cases

JK
Jan Koupil
Mon, Sep 21, 2020 9:00 PM

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

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
J
jon
Mon, Sep 21, 2020 9:26 PM

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

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 > >
A
adrianv
Mon, Sep 21, 2020 10:00 PM

You can structure your code in openscad something like this.  (Warning, not
tested.)

function exists(i) =
(i<0 || i>161) ? false
:  (i>0 && i<17) ? false
: ( i>19 && i<30) ? false
: (i>37 && i<48) ? false
: true;

It's a simple mapping from the C code.  I'm not sure what you mean about
161 cases?  The ternary operator can create a cascading if then else
structure, just like in C.

janek 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

Discuss@.openscad

You can structure your code in openscad something like this. (Warning, not tested.) function exists(i) = (i<0 || i>161) ? false : (i>0 && i<17) ? false : ( i>19 && i<30) ? false : (i>37 && i<48) ? false : true; It's a simple mapping from the C code. I'm not sure what you mean about 161 cases? The ternary operator can create a cascading if then else structure, just like in C. janek 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 > Discuss@.openscad > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org -- Sent from: http://forum.openscad.org/
JB
Jordan Brown
Mon, Sep 21, 2020 10:10 PM

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.

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.
A
arnholm@arnholm.org
Tue, Sep 22, 2020 4:21 AM

On 2020-09-21 23:00, 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
}

in AngelCAD your function would be

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

Carsten Arnholm

On 2020-09-21 23:00, 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 > } in AngelCAD your function would be bool 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 } Carsten Arnholm
RP
Ronaldo Persiano
Tue, Sep 22, 2020 1:39 PM

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)); // true
echo(exists(47)); // false

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

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)); // true echo(exists(47)); // false Although 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 < openscad@jordan.maileater.net> 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 > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org >