each - Assertion failed on flat list

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

each - Assertion failed on flat list

Parkinbot
@Marius. The following is nonsense from a semantical point of view, but it
shouldn't fail.

a = [each for(i=[1]) i];



--
Sent from: http://forum.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: each - Assertion failed on flat list

runsun
shouldn't 'each' be always followed by a list ?



-----

$  Runsun Pan, PhD $ libs: scadx , doctest , faces ( git ), offline doc ( git ), runscad.py ( 2 , git ), editor of choice: CudaText  ( OpenSCAD lexer ); $ Tips ; $ Snippets

--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
$ 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: each - Assertion failed on flat list

doug.moen
Yes, but OpenSCAD shouldn't crash on a syntax error.

On 27 April 2018 at 18:17, runsun <[hidden email]> wrote:
shouldn't 'each' be always followed by a list ?



-----

$  Runsun Pan, PhD $ libs: scadx , doctest , faces ( git ), offline doc ( git ), runscad.py ( 2 , git ), editor of choice: CudaText  ( OpenSCAD lexer );&nbsp;$ Tips ;&nbsp;$ Snippets

--
Sent from: http://forum.openscad.org/

_______________________________________________
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: each - Assertion failed on flat list

Parkinbot
In reply to this post by runsun
@runsun in general the parser doesn't know what a for loop will return. Thus
it is not a syntax error

E.g. this is correct:

a = [each for(i=[1,2]) [i]];





--
Sent from: http://forum.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: each - Assertion failed on flat list

nophead
Or this

a = [each [for(i=[1]) i]];

Rather than assert should it return undef or just return the scalar?

On 28 April 2018 at 00:28, Parkinbot <[hidden email]> wrote:
@runsun in general the parser doesn't know what a for loop will return. Thus
it is not a syntax error

E.g. this is correct:

a = [each for(i=[1,2]) [i]];





--
Sent from: http://forum.openscad.org/

_______________________________________________
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: each - Assertion failed on flat list

Parkinbot
I would expect a run time error.



--
Sent from: http://forum.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: each - Assertion failed on flat list

kintel
Administrator
In reply to this post by Parkinbot

> On Apr 27, 2018, at 5:55 PM, Parkinbot <[hidden email]> wrote:
>
> @Marius. The following is nonsense from a semantical point of view, but it
> shouldn't fail.
>
> a = [each for(i=[1]) i];
>
This is covered by https://github.com/openscad/openscad/issues/1851

 -Marius


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
tp3
Reply | Threaded
Open this post in threaded view
|

Re: each - Assertion failed on flat list

tp3
In reply to this post by Parkinbot
On 04/28/2018 10:58 AM, Parkinbot wrote:
> I would expect a run time error.
>
Why? What's the benefit of making it an error?

The proposal for each reads:
https://github.com/doug-moen/openscad2/blob/master/rfc/Generators.md

 > The each operator is new: it takes a sequence value
 > as argument, and adds each element to the list being
 > constructed. each x is equivalent to for(i=x)i.
 >
So according to that, both lines should be identical:

echo([for(i=3) i]);
echo([each 3]);

The first one currently gives
ECHO: [3]

ciao,
   Torsten.

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
-- Torsten
Reply | Threaded
Open this post in threaded view
|

Re: each - Assertion failed on flat list

Parkinbot
Torsten,

I wanted to say, I'd rather expect a runtime error instead of a failure! But
you are right, "each" can be implemented to not fail in this situation. Thus

a = [each for(i=[1:10]) i];

should be equivalent to

a = [for(i=[1:10]) i];

Why is this important? Because I want to write e.g.  
 
a = [each for(i=[1:10]) myfunc(i)];

and there is no guarantee that e.g. myfunc() will return only list elements.






--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
tp3
Reply | Threaded
Open this post in threaded view
|

Re: each - Assertion failed on flat list

tp3
Yes, that's what I was thinking too.

Test cases:
https://github.com/openscad/openscad/blob/a9a4ab2dba80bedddbbe4b6ae84238bce5913d89/testdata/scad/bugs/issue1851-each-fail-on-scalar.scad

echo([each for(i=1) i]);
echo([each for(i=[1,2]) i]);
echo([each for(i=[1,2,[3,4]]) i]);
echo([each for(i=[1,2,[3,4,[5,6]]]) i]);
echo([each each for(i=[1,2,[3,4,[5,6]]]) i]);
echo([each each for(i=[1,2,[3,4,[5,6,[7,8]]]]) i]);
echo([each each each for(i=[1,2,[3,4,[5,6,[7,8]]]]) i]);

Output:
https://github.com/openscad/openscad/blob/a9a4ab2dba80bedddbbe4b6ae84238bce5913d89/tests/regression/echotest/issue1851-each-fail-on-scalar-expected.echo

ECHO: [1]
ECHO: [1, 2]
ECHO: [1, 2, 3, 4]
ECHO: [1, 2, 3, 4, [5, 6]]
ECHO: [1, 2, 3, 4, 5, 6]
ECHO: [1, 2, 3, 4, 5, 6, [7, 8]]
ECHO: [1, 2, 3, 4, 5, 6, 7, 8]


Any other cases I may have missed?

ciao,
   Torsten.

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
-- Torsten
Reply | Threaded
Open this post in threaded view
|

Re: each - Assertion failed on flat list

Parkinbot
It is a question of semantics.

The avoid-runtime-errors-rule would be (in pseudo code):

 if (isscalar(value)) value;
 else flatten(value);

and it would flatten down scalars and non-nested lists to the same level. Is
this desired?

Implementing the runtime-errors-rule would be more strict:

assert(!isscalar(value)) flatten(value);

 




--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org