[OpenSCAD] Idea: local var inside list_comprehension

nop head nop.head at gmail.com
Mon Apr 20 16:17:26 EDT 2015


Better to optimise tail recursion to be iteration than make the language
more complicated and ugly. I thought that had already been done though?

On 20 April 2015 at 20:39, runsun <runsun at gmail.com> wrote:

> 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.
>
> _______________________________________________
> OpenSCAD mailing list
> Discuss at lists.openscad.org
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscad.org/pipermail/discuss_lists.openscad.org/attachments/20150420/e7c79d4f/attachment-0002.html>


More information about the Discuss mailing list