[OpenSCAD] Openscad Indirect Functions

Richard Urwin soronlin+openscad at googlemail.com
Mon Oct 17 07:58:38 EDT 2016


ottojas wrote
> Here is an example of openScad code using the new syntax.
> 
> function reduce(func,vec) =
>      len(vec)==2 ? @func(vec[0],vec[1]) : @func(vec[0],reduce(func,[for
> (i=[1:len(vec)-1]) vec[i]])); 
> 
> echo(reduce(@(x,y:x+y),[1,2,3,4,5]));
> echo(reduce(@(x,y:x*y),[1,2,3,4,5]));
>      
> cossin=@(x,y:sin(x)-cos(y));
> echo(reduce(cossin,[1,2,3,4,5]));     
>       
> /**************************
>      RESULT
> ECHO: 15
> ECHO: 120
> ECHO: -0.982406     
> **************************/  

The @ in the indirect call is required for backwards compatibility but every
other use of @ could be replaced by "function", making your code:

function reduce(func,vec) =
     len(vec)==2 ? @func(vec[0],vec[1]) : @func(vec[0],reduce(@func,[for
(i=[1:len(vec)-1]) vec[i]])); 

echo(reduce(function(x,y:x+y),[1,2,3,4,5]));
echo(reduce(function(x,y:x*y),[1,2,3,4,5]));
     
cossin=function(x,y:sin(x)-cos(y));
echo(reduce(cossin,[1,2,3,4,5]));     
      

To my mind, that is more readable.
Then, if eventually the namespaces are merged, the @ could be deprecated and
phased out.



--
View this message in context: http://forum.openscad.org/Convert-from-object-to-polygon-polyhedron-tp18522p18752.html
Sent from the OpenSCAD mailing list archive at Nabble.com.




More information about the Discuss mailing list