Module return

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

Module return

ashuan
Hello alls,

I'm a newbie to OpenScad but not to programing language.
I have a blocking point about module return.
I have a look about this forum without finded answer.

I my preferated world, i wish have a syntaxe like this :

module <name> (<parameter1>, <parameter2>, ..., <parameterN>) {
   <instruction1>;
   <instruction2>;
   ...
   <instructionN>;
   <name> = <return value>; // << or return <return value> or result <return
value> or ...
}

an example that is useless if it is not to illustrate my purpose :
module inList(searchText, list)
{
    resultat = false;
    if (len(search(searchText, list)== 0))
    {
        resultat = true;
    }
    inList = resultat;
}

Could you help me, please ?




--
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: Module return

Gadgetmind
On 2017-10-20 09:29, ashuan wrote:
>     <name> = <return value>; // << or return <return value> or result <return
> value> or ...

All OpenSCAD modules can "return" is geometry.

OpenSCAD functions can return values including vectors and strings, but
coding inside functions is rather different to in modules. You'll
probably want to make heavy use of the let () element, and either the ?
: method of choosing what to return or the new ifelse described here.

https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/WIP#if.2Felse

function inList(searchText, list) =
        let ( result = search(searchText, list, 0))
     len(result) == 0 ? true : result;
 

echo ( inList ("a", "abcdabcd") );

Note that the vector returned from search doesn't have the length you might expect if the result isn't found with num_returns_per_match=0, so the example isn't great.


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

Re: Module return

Eric Buijs
In reply to this post by ashuan
OpenSCAD is a functional programming language and contrary to imperative
programming you can't reassign a new value to a variable. Something I
struggled with in the beginning. For better understanding I wrote an short
article on this
http://eribuijs.blogspot.nl/2017/09/openscad-functional-programming.html
<http://eribuijs.blogspot.nl/2017/09/openscad-functional-programming.html>
. Hopefully it's useful.

In OpenSCAD a *module* is generally used to define objects but *functions*
operate on values an return values. So you need a function. Luckily OpenSCAD
already has a built-in function search that we can use in your case. Below
is an example that may be applicable to your situation.

lst = "abcdabcd";
function st_search (st,lst) = (search(st,lst) != []) ? true : false;
echo(st_search("a",lst));



--
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: Module return

ashuan
In reply to this post by Gadgetmind
Thanks for this answer



--
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: Module return

nophead
In reply to this post by ashuan
Modules create or transform geometry, they don't return values and can't be used in expressions. You need a function for that.

function inList(searchText, list) = len(search(searchText, list)) == 0;

On 20 October 2017 at 09:29, ashuan <[hidden email]> wrote:
Hello alls,

I'm a newbie to OpenScad but not to programing language.
I have a blocking point about module return.
I have a look about this forum without finded answer.

I my preferated world, i wish have a syntaxe like this :

module <name> (<parameter1>, <parameter2>, ..., <parameterN>) {
   <instruction1>;
   <instruction2>;
   ...
   <instructionN>;
   <name> = <return value>; // << or return <return value> or result <return
value> or ...
}

an example that is useless if it is not to illustrate my purpose :
module inList(searchText, list)
{
    resultat = false;
    if (len(search(searchText, list)== 0))
    {
        resultat = true;
    }
    inList = resultat;
}

Could you help me, please ?




--
Sent from: http://forum.openscad.org/

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

Re: Module return

Tim V. Shaporev
Wow! You know Boolean logic!
As was really desperate looking on "? true : false"
:-|

On 10/20/2017 2:17 PM, nop head wrote:

> Modules create or transform geometry, they don't return values and can't
> be used in expressions. You need a function for that.
>
> function inList(searchText, list) = len(search(searchText, list)) == 0;
>
> On 20 October 2017 at 09:29, ashuan <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     Hello alls,
>
>     I'm a newbie to OpenScad but not to programing language.
>     I have a blocking point about module return.
>     I have a look about this forum without finded answer.
>
>     I my preferated world, i wish have a syntaxe like this :
>
>     module <name> (<parameter1>, <parameter2>, ..., <parameterN>) {
>         <instruction1>;
>         <instruction2>;
>         ...
>         <instructionN>;
>         <name> = <return value>; // << or return <return value> or
>     result <return
>     value> or ...
>     }
>
>     an example that is useless if it is not to illustrate my purpose :
>     module inList(searchText, list)
>     {
>          resultat = false;
>          if (len(search(searchText, list)== 0))
>          {
>              resultat = true;
>          }
>          inList = resultat;
>     }
>
>     Could you help me, please ?
>
>
>
>
>     --
>     Sent from: http://forum.openscad.org/
>
>     _______________________________________________
>     OpenSCAD mailing list
>     [hidden email] <mailto:[hidden email]>
>     http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org <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
>


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

Re: Module return

ashuan
In reply to this post by nophead
Thanks all for your explanation.

I've understand now the positionnement logic of module.

I'll try your 3 examples without success.

For understanding, i've this call :

// variables
modulesActifs = ["verin", "title"]; // vague

// affiche un système de vague coloré
if (inList("vague", modulesActifs))
{
    ...





--
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: Module return

ashuan
In reply to this post by Gadgetmind
I'm use customizer plugin, it's very nice.



--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org