discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

flattening mixed lists of lists

K
kitwallace
Mon, Apr 6, 2015 9:52 AM

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.

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.
NH
nop head
Mon, Apr 6, 2015 10:53 AM

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@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@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

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@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@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org > >
K
kitwallace
Mon, Apr 6, 2015 11:11 AM

Well, blow me!  So near and yet so far! Many thanks Chris

--
View this message in context: http://forum.openscad.org/flattening-mixed-lists-of-lists-tp12316p12318.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Well, blow me! So near and yet so far! Many thanks Chris -- View this message in context: http://forum.openscad.org/flattening-mixed-lists-of-lists-tp12316p12318.html Sent from the OpenSCAD mailing list archive at Nabble.com.
R
runsun
Sun, Apr 12, 2015 7:02 AM

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
$ -- OpenScad_DocTest: doc and unit test ( Github , Thingiverse  )
$ -- Linux Mint 17.1 Rebecca x64  + OpenSCAD 2015.03.15
$ -- Linux Mint 17.1 Rebecca x64  + OpenSCAD 2015.04.01.nightly

--
View this message in context: http://forum.openscad.org/flattening-mixed-lists-of-lists-tp12316p12347.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

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 $ -- OpenScad_DocTest: doc and unit test ( Github , Thingiverse ) $ -- Linux Mint 17.1 Rebecca x64 + OpenSCAD 2015.03.15 $ -- Linux Mint 17.1 Rebecca x64 + OpenSCAD 2015.04.01.nightly -- View this message in context: http://forum.openscad.org/flattening-mixed-lists-of-lists-tp12316p12347.html Sent from the OpenSCAD mailing list archive at Nabble.com.
DM
doug moen
Sun, Apr 12, 2015 4:25 PM

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 kit.wallace@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:
http://forum.openscad.org/flattening-mixed-lists-of-lists-tp12316.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

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 <kit.wallace@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: > http://forum.openscad.org/flattening-mixed-lists-of-lists-tp12316.html > Sent from the OpenSCAD mailing list archive at Nabble.com.
K
kitwallace
Mon, Apr 13, 2015 6:54 AM

Hi Doug

The code is here -
https://github.com/KitWallace/openscad/blob/master/poly_nets.scad

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

http://kitwallace.tumblr.com/post/115973599619/polyhedral-nets

Chris

On Sun, Apr 12, 2015 at 5:26 PM, doug.moen [via OpenSCAD] <
ml-node+s1091067n12350h42@n5.nabble.com> 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]
http:///user/SendEmail.jtp?type=node&node=12350&i=0> 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:///user/SendEmail.jtp?type=node&node=12350&i=1
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
http://forum.openscad.org/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=12316&code=a2l0LndhbGxhY2VAZ21haWwuY29tfDEyMzE2fDE3Nzk2Mzg2MzQ=
.
NAML
http://forum.openscad.org/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml

--
View this message in context: http://forum.openscad.org/flattening-mixed-lists-of-lists-tp12316p12352.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Hi Doug The code is here - https://github.com/KitWallace/openscad/blob/master/poly_nets.scad 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 http://kitwallace.tumblr.com/post/115973599619/polyhedral-nets Chris On Sun, Apr 12, 2015 at 5:26 PM, doug.moen [via OpenSCAD] < ml-node+s1091067n12350h42@n5.nabble.com> 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] > <http:///user/SendEmail.jtp?type=node&node=12350&i=0>> 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:///user/SendEmail.jtp?type=node&node=12350&i=1> > 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 > <http://forum.openscad.org/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=12316&code=a2l0LndhbGxhY2VAZ21haWwuY29tfDEyMzE2fDE3Nzk2Mzg2MzQ=> > . > NAML > <http://forum.openscad.org/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml> > -- View this message in context: http://forum.openscad.org/flattening-mixed-lists-of-lists-tp12316p12352.html Sent from the OpenSCAD mailing list archive at Nabble.com.