[OpenSCAD] Openscad Indirect Functions

otto otto at 123phase.com
Mon Oct 10 17:15:13 EDT 2016


Let me summarize current situation:

The following work in my current code:

function map(func,vec) = [for (i=vec) @func(i)];
function @xsq(x) = x*x;
function recip(x)=1/x;
box = "recip";

vec1=[1, 2, 3];
map(box,vec);
map(xsq,vec);

Objections have been:
  1. First version using "function @xsq(x)" seems kludgy.
  2. One suggestion was the following syntax:
> function recip(x)=1/x;
> vec1=[1, 2, 3];
> function map(func,vec) = [for (i=vec) func(i)];
   which seems intuitive.
  3. (my own objections)  None of these allow for lambda like syntax.

I have a particular problem with my own current code and all
suggestions.  They fail to implement lambda. Suggestion 2 is
essentially the same way that python deals with indirection, Python
provides two methods of creating a function. One method uses a def
statement and the other uses the Lambda construct. They are equivalent
and dereferencing is automatic.

I believe I can get the lambda behavior relatively easily in the current
system. To add lambda syntax I will implement it as:

 @(<argumentlist>:<functionDefintion>)
 
which I think is easier to read than (from python):

lambda <argumentlist>:<functionDefintion>

Either implementation is possible and if enough people request one over
the other, I am willing to implement either.  The first is much simpler
to add for me with my current shallow knowledge of how the code works
and really requires very small changes.

The following would then work:

vec1=[1, 2, 3];
function map(func,vec) = [for (i=vec) @func(i)];
recip = @(x:1/x);
map(recip,vec); //this would work as before
map(@(x,x*x),vec); //this would be using @ as lambda.

I actually believe that the dereferencing of a variable name using "@"
makes the code clearer and easier to maintain and we lose nothing in 
terms of capabilities.  

If anyone wants to hack the code themselves to obtain any behavior, I
am happy to help point out where in the code changes can be made.
Currently there are only about 30 lines of code that have been added. In
addition, there are some problems currently in my code that could use
some work. I will post these in openscad-affine when I have time.
Collaboration on this is desirable. Incidentally I need these changes
for my work.  The modules in paraSolids are essential for me and need
the function indirection capabilities to work.

For examples of pretty picture please see: ParaSolidExamples.pdf
which is among the files at https://github.com/ottojas/openscad-affine

The repo needs some re-arrangement but I think everything is there..

Thoughts?

I would really like it if someone would test the code I have written.
I am almost exclusively on debian based linux systems.  Debian, Ubuntu
and mint.  I would be happy to build a binary for one of these if that
is what someone willing to test would like.  I am definitely going to
implement the second syntax when i have time (next week at earliest),
that is the "@(<argument list>:<function definition>)" syntax for an
anonymous function.

If requested, I will fork the complete source code setup so you can
compile on any system.  Is that a good idea??

Regards
Otto








On Mon, 10 Oct 2016 12:15:31 -0700 (MST)
Bananapeel <lunatica.xiaoyu at gmail.com> wrote:

> I don't know why this thread is inside the thread "Convert from
> object to polygon/polyhedron." Anyways, regarding passing functions
> as arguments:
> 
> IMO, there's just one "proper" syntax for this. And that is, that the
> name without () acts as a variable, and () means call. And function
> becomes just another type, like string and int. This means functions
> and variables must occupy the same namespace. This syntax can also
> work for modules, however, this means variables, functions and
> modules all share the same namespace, which currently isn't the case.
> 
> Using () on a variable that isn't of type function or module would
> give a runtime error.
> 
> Example:
> 
> //Preferred usage//
> function xsq(x) = x*x;
> function recip(x)=1/x;
> vec1=[1, 2, 3];
> vec2=[0.1, 0.7];
> 
> function map(func,vec) = [for (i=vec) func(i)];
> echo(map(xsq,vec1));
> echo(map(xsq,vec2));
> echo(map(recip,vec1));
> echo(map(recip,vec2));
> 
> //Other crazy usage//
> function div3(x)=x/3;
> indirect = div3;
> echo(map(indirect,vec2)); 
> 
> 
> 
> 
> --
> View this message in context:
> http://forum.openscad.org/Convert-from-object-to-polygon-polyhedron-tp18522p18659.html
> Sent from the OpenSCAD mailing list archive at Nabble.com.
> 
> _______________________________________________
> OpenSCAD mailing list
> Discuss at lists.openscad.org
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org





More information about the Discuss mailing list