Function with logic/cases

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Function with logic/cases

janek
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
Reply | Threaded
Open this post in threaded view
|

Re: Function with logic/cases

jon_bondy
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
Reply | Threaded
Open this post in threaded view
|

Re: Function with logic/cases

adrianv
In reply to this post by janek
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/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Function with logic/cases

JordanBrown
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
Reply | Threaded
Open this post in threaded view
|

Re: Function with logic/cases

cacb
In reply to this post by janek
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

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Function with logic/cases

Ronaldo
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)); // 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 <[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