flattening mixed lists of lists

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

flattening mixed lists of lists

kitwallace
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.
Reply | Threaded
Open this post in threaded view
|

Re: flattening mixed lists of lists

nophead
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 <[hidden email]> 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
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
Reply | Threaded
Open this post in threaded view
|

Re: flattening mixed lists of lists

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

Re: flattening mixed lists of lists

runsun
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
Reply | Threaded
Open this post in threaded view
|

Re: flattening mixed lists of lists

doug.moen
In reply to this post by kitwallace
Kit, could you show your code that generates the face list?

I'd like to see it, because I'm considering an extension to list
comprehensions that would eliminate the need to use 'flatten'
functions for post processing. I'd like to see if my proposed
extension works for your code.

Doug Moen

On 06/04/2015, kitwallace <[hidden email]> 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:
> http://forum.openscad.org/flattening-mixed-lists-of-lists-tp12316.html
> 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
Reply | Threaded
Open this post in threaded view
|

Re: flattening mixed lists of lists

kitwallace
Hi Doug


faces are constructed in function rrp (line 234) as a nested list and flattened in fold_render(line 256) using function dflatten() 

The context of the code is described in my blog - basically  it generates nets of solids and folds them


Chris


On Sun, Apr 12, 2015 at 5:26 PM, doug.moen [via OpenSCAD] <[hidden email]> wrote:
Kit, could you show your code that generates the face list?

I'd like to see it, because I'm considering an extension to list
comprehensions that would eliminate the need to use 'flatten'
functions for post processing. I'd like to see if my proposed
extension works for your code.

Doug Moen

On 06/04/2015, kitwallace <[hidden email]> 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:
> http://forum.openscad.org/flattening-mixed-lists-of-lists-tp12316.html
> 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



If you reply to this email, your message will be added to the discussion below:
http://forum.openscad.org/flattening-mixed-lists-of-lists-tp12316p12350.html
To unsubscribe from flattening mixed lists of lists, click here.
NAML