[OpenSCAD] flattening mixed lists of lists

nop head nop.head at gmail.com
Mon Apr 6 06:53:16 EDT 2015


Perhaps simply this:

function depth(a) =
   len(a)== undef
       ? 0
       : 1+depth(a[0]);

function flatten(l) = [ for (a = l) for (b = a) b ] ;

function dflatten(l,d=2) =
// hack to flatten mixed list and list of lists
    flatten([for (a = l) depth(a) > d ? dflatten(a, d) : [a]]);

l= [  [[1,2],[2,3]], [[[1,2],[2,3]]] ];
lf = dflatten(l,1);
echo(len(lf),lf);
ECHO: 4, [[1, 2], [2, 3], [1, 2], [2, 3]]





On 6 April 2015 at 10:52, kitwallace <kit.wallace at gmail.com> wrote:

> I'm generating faces as a list of points. However the list of faces
> comprises a mixture of faces and lists of faces. I need to flatten this
> mixed list. So far I'm using
>
> function depth(a) =
>    len(a)== undef
>        ? 0
>        : 1+depth(a[0]);
>
> function flatten(l) = [ for (a = l) for (b = a) b ] ;
>
> function dflatten(l,d=2) =
> // hack to flatten mixed list and list of lists
>     flatten([for (a = l) depth(a) > d ? [for (b=a) b] : [a]]);
>
> l= [  [[1,2],[2,3]], [[[1,2],[2,3]]] ];
> lf = dflatten(l);
> echo(len(lf),lf);
>
> but it doesn't really work generally : d = 1 should yield 4 , not 3 I've
> really struggled to come up with a correct recursive formulation.
> ------------------------------
> View this message in context: flattening mixed lists of lists
> <http://forum.openscad.org/flattening-mixed-lists-of-lists-tp12316.html>
> Sent from the OpenSCAD mailing list archive <http://forum.openscad.org/>
> 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/20150406/79ee9a9c/attachment-0002.html>


More information about the Discuss mailing list