[OpenSCAD] Openscad Indirect Functions

Bananapeel lunatica.xiaoyu at gmail.com
Mon Oct 10 15:15:31 EDT 2016


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.




More information about the Discuss mailing list