[OpenSCAD] Idea: local var inside list_comprehension

runsun runsun at gmail.com
Mon Apr 20 15:39:40 EDT 2015

I've carried out some speed tests to compare the performance of list
comprehension and recursion. The preliminary result shows that list comp. is
much much faster than recursion.  The difference is huge especially at large
data set. In some cases recursion hangs the computer but list comp can
finish in 1~2 sec.

But there are cases that recursion is needed. A simple sum( list ) function
will require recursion, because we can't carry the result of
list[i]+list[i+1] to the next iteration.

So I have an idea: how about an internal variable to represent the result of
the previous iteration ?

Let's say, @ = result of the previous iteration

function sum( list ) = [ for( i=[0:len(list)-1] ) i==0?list[i]:(@+list[i]) ]
                                 [ len(list)-1] ;

sum( [2,3,4] ) = [ 2,5,9][2] = 9;


$  Runsun Pan, PhD 

$ -- OpenScad_DocTest: doc and unit test ( Github , Thingiverse  ) 

$ -- hash parameter model: here , here 

$ -- Linux Mint 17.1 Rebecca x64  + OpenSCAD 2015.03.15/2015.04.01.nightly 

View this message in context: http://forum.openscad.org/Idea-local-var-inside-list-comprehension-tp12446.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

More information about the Discuss mailing list