# [OpenSCAD] flattening mixed lists of lists

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
> at Nabble.com.
>
> _______________________________________________