# flattening mixed lists of lists Classic List Threaded 6 messages Open this post in threaded view
|

## flattening mixed lists of lists

 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); 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.
Open this post in threaded view
|

## Re: flattening mixed lists of lists

 Perhaps simply this: function depth(a) =   len(a)== undef        ? 0       : 1+depth(a);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 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); 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 Sent from the OpenSCAD mailing list archive at Nabble.com._______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: flattening mixed lists of lists

 Well, blow me!  So near and yet so far! Many thanks Chris
Open this post in threaded view
|

## Re: flattening mixed lists of lists

 A full flattening can be achieved with: ``````function flatten(a)= [ for(x=a) for(b=len(x)==undef? x:flatten(x) ) b ]; a=[ [[1,2],[2,3]], [[[1,2],[2,3]]] ]; echo( flatten(a) ); //ECHO: [1, 2, 2, 3, 1, 2, 2, 3] `````` We can then add a `len(x)==2` to return a list of pairs: ``````function flatten2(a)= [ for(x=a) for(b=(len(x)==undef) || (len(x)==2) ?x:flatten2(x) ) b ]; echo( flatten2(a) ); //ECHO: [[1, 2], [2, 3], [1, 2], [2, 3]] `````` Note both will fail if flattening a more general list (that might contain strings). Adding a `str(x)==x` would help: ``````function flatten3(a)= [ for(x=a) for(b= (str(x)==x) || len(x)==undef? x:flatten3(x) ) b ]; b=[ ["abc", ["def","g"]],"h"]; echo( flatten3(b) ); //ECHO: ["abc", "def", "g", "h"] `````` \$ Runsun Pan, PhD \$ libs: scadx, doctest, faces(git), offline doc(git), runscad.py(2,git), editor of choice: CudaText ( OpenSCAD lexer); \$ Tips; \$ Snippets