Breaking away from a list comprehension ?

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

Breaking away from a list comprehension ?

runsun
Testing the new list comprehension:

function test(arr)=
[ for (x=arr)
        if(x!=0) x ];

echo(test( [2,3,4,5] )); // [2,3,4,5]
echo(test( [2,3,0,5] )); // [2,3,5]

Is there any way to break away from the iteration, and give an answer like  [2,3] in the above case ?

$ 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: Breaking away from a list comprehension ?

MichaelAtOz
Administrator
I dont think so, but I'd use a recursion to do that.


function stopAt(arr,item,_i=0) =
        ( _i==len(arr) || arr[_i] == item )
        ? []
        : concat(arr[_i], stopAt(arr,item,_i+1));

v= [ 1,2,3,4,0,5,6];

echo(stopAt(v,0));
Admin - email* me if you need anything, or if I've done something stupid...
* click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work.
Obviously inclusion of works of previous authors is not included in the above.
Reply | Threaded
Open this post in threaded view
|

Re: Breaking away from a list comprehension ?

Oskar
Michaels solution is the correct way to solve this. You should not consider the for() in list comprehensions a iterative construct. It is a definition of an ordered set; think of it as for_all().

Best,

Oskar
Reply | Threaded
Open this post in threaded view
|

Re: Breaking away from a list comprehension ?

runsun
Thx both Michael and Oskar for the reply. Thought we could get rid of recursion, ha.

$ 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: Breaking away from a list comprehension ?

nophead
No it's the other way round. With recursion you don't need iteration.


On 6 August 2014 20:44, runsun <[hidden email]> wrote:
Thx both Michael and Oskar for the reply. Thought we could get rid of
recursion, ha.





-----
$ Runsun Pan, PhD
$ -- OpenScad_DocTest: doc and unit test ( Github , Thingiverse  )
$ -- Linux Mint 17 MATE 64bit  + OpenSCAD 2014.05.31 snapshot
$ -- Linux Mint 17 MATE 64bit + Wine 1.6.2 + OpenSCAD for Windows 2014.07.22 snapshot


--
View this message in context: http://forum.openscad.org/Breaking-away-from-a-list-comprehension-tp9231p9244.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Breaking away from a list comprehension ?

kintel
Administrator
In reply to this post by runsun

On Aug 6, 2014, at 00:24 AM, runsun <[hidden email]> wrote:

>
> Is there any way to break away from the iteration, and give an answer like
> [2,3] in the above case ?
>
You can always just iterate over indices:

 function test(arr)= [ for (i=[0:len(arr)-1]) if(i < 2) arr[i] ];

 -Marius

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Breaking away from a list comprehension ?

runsun
In reply to this post by nophead
>>> No it's the other way round. With recursion you don't need iteration.

Doesn't recursion introduce more function call overheads ?


On Wed, Aug 6, 2014 at 3:05 PM, nophead [via OpenSCAD] <[hidden email]> wrote:
No it's the other way round. With recursion you don't need iteration.


On 6 August 2014 20:44, runsun <[hidden email]> wrote:
Thx both Michael and Oskar for the reply. Thought we could get rid of
recursion, ha.





-----
$ Runsun Pan, PhD
$ -- OpenScad_DocTest: doc and unit test ( Github , Thingiverse  )

$ -- Linux Mint 17 MATE 64bit  + OpenSCAD 2014.05.31 snapshot
$ -- Linux Mint 17 MATE 64bit + Wine 1.6.2 + OpenSCAD for Windows 2014.07.22 snapshot


--
View this message in context: http://forum.openscad.org/Breaking-away-from-a-list-comprehension-tp9231p9244.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566


If you reply to this email, your message will be added to the discussion below:
http://forum.openscad.org/Breaking-away-from-a-list-comprehension-tp9231p9245.html
To unsubscribe from Breaking away from a list comprehension ?, click here.
NAML



--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Runsun Pan, PhD
[Fax] 832-534-3213
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$ 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: Breaking away from a list comprehension ?

runsun
In reply to this post by kintel
>>> function test(arr)= [ for (i=[0:len(arr)-1]) if(i < 2) arr[i] ];

In this solution, the condition, i<2, is known at the time of coding. In the case I mentioned, the condition can only be known at the run-time. So it's slightly different.

Sure we can make a function, index(arr,item) for this:

ii = index( arr, 0 );  // chk where 0 is in the array;

then do this:

function test(arr)= [ for (i=[0:len(arr)-1]) if(i <= index(arr,0) ) arr[i] ];


but that would mean we go through the array twice, one in the index(), one in the for-loop.


On Sat, Aug 9, 2014 at 11:41 PM, kintel [via OpenSCAD] <[hidden email]> wrote:

On Aug 6, 2014, at 00:24 AM, runsun <[hidden email]> wrote:

>
> Is there any way to break away from the iteration, and give an answer like
> [2,3] in the above case ?
>
You can always just iterate over indices:

 function test(arr)= [ for (i=[0:len(arr)-1]) if(i < 2) arr[i] ];

 -Marius

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566



If you reply to this email, your message will be added to the discussion below:
http://forum.openscad.org/Breaking-away-from-a-list-comprehension-tp9231p9269.html
To unsubscribe from Breaking away from a list comprehension ?, click here.
NAML



--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Runsun Pan, PhD
[Fax] 832-534-3213
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$ 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: Breaking away from a list comprehension ?

nophead
In reply to this post by runsun
On 10 August 2014 17:16, runsun <[hidden email]> wrote:
>>> No it's the other way round. With recursion you don't need iteration.

Doesn't recursion introduce more function call overheads ?

It does in a simple implementation but it can optimised away, see http://en.wikipedia.org/wiki/Recursion_%28computer_science%29#Recursion_versus_iteration

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Breaking away from a list comprehension ?

runsun
Thx nophead for the link. It's a good read. Need time to digest but at least a quick browsing teaches me what a tail recursion is and it could help in performance.
$ Runsun Pan, PhD
$ libs: scadx, doctest, faces(git), offline doc(git), runscad.py(2,git), editor of choice: CudaText ( OpenSCAD lexer); $ Tips; $ Snippets