R
runsun
Sun, Oct 30, 2016 10:50 AM
list = [ for (a = [ 1 : 8 ]) if (a % 2 == 0) a else a*10 ];
echo(list); // ECHO: [10, 2, 30, 4, 50, 6, 70, 8]
But I am a bit wondering what's benefit of it over this shorter one:
list = [ for (a = [ 1 : 8 ]) (a % 2 == 0)? a : a*10 ];
I can't figure out the other two and will need help.
$ Runsun Pan, PhD $ libs: doctest , faces ( git ), offline doc ( git ), runscad.py ( 2 , git ), synwrite ( 2 ); $ tips: hash ( 2 ), matrix ( 2 , 3 ), sweep ( 2 , 3 ), var ( 2 ), lerp , animation ( gif , prodVid , animlib ), precision ( 2 ), xl-control , type , rounded polygon , chfont , tailRecur ( 2, 3 ), isosphere ( 2 ), area , vol/center , RGB , CurvedImg , tests ( 2 ); $ Apps: rollApp , blockscad , openjscad , on AWS ( pdf )
View this message in context: http://forum.openscad.org/Any-where-to-find-the-doc-for-lc-each-lc-else-and-lc-for-c-tp18823.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
Trying out the newest snapshot 2016.10.04 (git 7e0935d) and found these new
features: lc-each, lc-else and lc-for-c
Can't seem to find docs/examples about them in list comprehension
<https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/List_Comprehensions>
doc.
The lc-else is easy to understand:
> list = [ for (a = [ 1 : 8 ]) if (a % 2 == 0) a else a*10 ];
> echo(list); // ECHO: [10, 2, 30, 4, 50, 6, 70, 8]
But I am a bit wondering what's benefit of it over this shorter one:
> list = [ for (a = [ 1 : 8 ]) (a % 2 == 0)? a : a*10 ];
I can't figure out the other two and will need help.
-----
$ Runsun Pan, PhD $ libs: doctest , faces ( git ), offline doc ( git ), runscad.py ( 2 , git ), synwrite ( 2 ); $ tips: hash ( 2 ), matrix ( 2 , 3 ), sweep ( 2 , 3 ), var ( 2 ), lerp , animation ( gif , prodVid , animlib ), precision ( 2 ), xl-control , type , rounded polygon , chfont , tailRecur ( 2, 3 ), isosphere ( 2 ), area , vol/center , RGB , CurvedImg , tests ( 2 ); $ Apps: rollApp , blockscad , openjscad , on AWS ( pdf )
--
View this message in context: http://forum.openscad.org/Any-where-to-find-the-doc-for-lc-each-lc-else-and-lc-for-c-tp18823.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
TP
Torsten Paul
Sun, Oct 30, 2016 10:55 AM
On 10/30/2016 11:50 AM, runsun wrote:
On 10/30/2016 11:50 AM, runsun wrote:
> Trying out the newest snapshot 2016.10.04 (git 7e0935d) and found these new
> features: lc-each, lc-else and lc-for-c
>
> Can't seem to find docs/examples about them in list comprehension
> <https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/List_Comprehensions>
> doc.
>
See https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/WIP
ciao,
Torsten.
R
Ronaldo
Sun, Oct 30, 2016 1:45 PM
But I am a bit wondering what's benefit of it over this shorter one:
list = [ for (a = [ 1 : 8 ]) (a % 2 == 0)? a : a*10 ];
There is benefit in some nested tests and filters:
list = [ for (a = [ 1 : 8 ]) if (a % 2 == 0) a else if (a!=1) a*10 ];
runsun wrote
> But I am a bit wondering what's benefit of it over this shorter one:
>> list = [ for (a = [ 1 : 8 ]) (a % 2 == 0)? a : a*10 ];
There is benefit in some nested tests and filters:
> list = [ for (a = [ 1 : 8 ]) if (a % 2 == 0) a else if (a!=1) a*10 ];
--
View this message in context: http://forum.openscad.org/Any-where-to-find-the-doc-for-lc-each-lc-else-and-lc-for-c-tp18823p18827.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
NH
nop head
Sun, Oct 30, 2016 3:02 PM
Presumably, by using each you could add a different numbers of elements in
the else part, which you can't do with a ? : expression.
On 30 October 2016 at 13:45, Ronaldo rcmpersiano@gmail.com wrote:
But I am a bit wondering what's benefit of it over this shorter one:
list = [ for (a = [ 1 : 8 ]) (a % 2 == 0)? a : a*10 ];
There is benefit in some nested tests and filters:
list = [ for (a = [ 1 : 8 ]) if (a % 2 == 0) a else if (a!=1) a*10 ];
Presumably, by using each you could add a different numbers of elements in
the else part, which you can't do with a ? : expression.
On 30 October 2016 at 13:45, Ronaldo <rcmpersiano@gmail.com> wrote:
> runsun wrote
> > But I am a bit wondering what's benefit of it over this shorter one:
> >> list = [ for (a = [ 1 : 8 ]) (a % 2 == 0)? a : a*10 ];
>
> There is benefit in some nested tests and filters:
>
> > list = [ for (a = [ 1 : 8 ]) if (a % 2 == 0) a else if (a!=1) a*10 ];
>
>
>
>
>
> --
> View this message in context: http://forum.openscad.org/Any-
> where-to-find-the-doc-for-lc-each-lc-else-and-lc-for-c-tp18823p18827.html
> Sent from the OpenSCAD mailing list archive at Nabble.com.
>
> _______________________________________________
> OpenSCAD mailing list
> Discuss@lists.openscad.org
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>
R
runsun
Mon, Oct 31, 2016 5:28 AM
Thx guys for the help. I tried out the examples in the WIP and you guys
provided. These are all very powerful additions. The for-else example in the
WIP was very confusing to me at first, until I tried the example Ronaldo
gave.
I think an easy way of explanation would be that a "cond?a:b" format handles
AND RETURN every single item, but "if" or "if-else" allows users to discard
some. With this in mind, the example in WIP,
echo([ for (a = [0 : 3]) if (a < 2) ( if (a < 1) ["+", a] ) else ["-", a]
]);
// ECHO: [["+", 0], ["-", 2], ["-", 3]]
when a<2: discard those a>=1, and handle the rest as ["+",a]
when a>=2: ["-",a]
Emphasizing the "discard" part helps me understand it better.
In my opinion, this would be the same as if we allow an "if" existing in the
"cond?a:b" format;
Or, for Ronado's example:
I'd like this form better, because it sort of focusing the feature of "if"
as a filter that discards something, but not "sometimes it discards,
sometimes it picks it back up when else is added".
But, at least it's there when we need it. Thx to those who puts them up.
Great additions.
$ Runsun Pan, PhD $ libs: doctest , faces ( git ), offline doc ( git ), runscad.py ( 2 , git ), synwrite ( 2 ); $ tips: hash ( 2 ), matrix ( 2 , 3 ), sweep ( 2 , 3 ), var ( 2 ), lerp , animation ( gif , prodVid , animlib ), precision ( 2 ), xl-control , type , rounded polygon , chfont , tailRecur ( 2, 3 ), isosphere ( 2 ), area , vol/center , RGB , CurvedImg , tests ( 2 ); $ Apps: rollApp , blockscad , openjscad , on AWS ( pdf )
View this message in context: http://forum.openscad.org/Any-where-to-find-the-doc-for-lc-each-lc-else-and-lc-for-c-tp18823p18834.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
Thx guys for the help. I tried out the examples in the WIP and you guys
provided. These are all very powerful additions. The for-else example in the
WIP was very confusing to me at first, until I tried the example Ronaldo
gave.
I think an easy way of explanation would be that a "cond?a:b" format handles
AND RETURN every single item, but "if" or "if-else" allows users to discard
some. With this in mind, the example in WIP,
> echo([ for (a = [0 : 3]) if (a < 2) ( if (a < 1) ["+", a] ) else ["-", a]
> ]);
> // ECHO: [["+", 0], ["-", 2], ["-", 3]]
can be read as:
>>> when a<2: discard those a>=1, and handle the rest as ["+",a]
>>> when a>=2: ["-",a]
Emphasizing the "discard" part helps me understand it better.
In my opinion, this would be the same as if we allow an "if" existing in the
"cond?a:b" format;
> a<2?
*
> ( if (a < 1) ["+", a] )
*
> : ["-", a]
Or, for Ronado's example:
> a %2==0? a:
*
> if (a!=1) a*10
*
I'd like this form better, because it sort of focusing the feature of "if"
as a filter that discards something, but not "sometimes it discards,
sometimes it picks it back up when else is added".
But, at least it's there when we need it. Thx to those who puts them up.
Great additions.
-----
$ Runsun Pan, PhD $ libs: doctest , faces ( git ), offline doc ( git ), runscad.py ( 2 , git ), synwrite ( 2 ); $ tips: hash ( 2 ), matrix ( 2 , 3 ), sweep ( 2 , 3 ), var ( 2 ), lerp , animation ( gif , prodVid , animlib ), precision ( 2 ), xl-control , type , rounded polygon , chfont , tailRecur ( 2, 3 ), isosphere ( 2 ), area , vol/center , RGB , CurvedImg , tests ( 2 ); $ Apps: rollApp , blockscad , openjscad , on AWS ( pdf )
--
View this message in context: http://forum.openscad.org/Any-where-to-find-the-doc-for-lc-each-lc-else-and-lc-for-c-tp18823p18834.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
R
Ronaldo
Mon, Oct 31, 2016 1:03 PM
if ( A ) ( if( B ) x else if (C) y ) else z
could be expressed in the old one by:
if ( B || C ) !A ? z : B ? x : y
which is rather cryptic and hard to write. Besides, if we change the first
code eliminating the 'else z' part, the standard code will change to:
if ( A ) if(B || C) B ? x: y
which is far from an obvious change.
runsun wrote
I'd like this form better, because it sort of focusing the feature of "if"
as a filter that discards something, but not "sometimes it discards,
sometimes it picks it back up when else is added".
I agree that the list comprehension 'if' is not always a filter in the new
syntax. But in your alternative the construct ?: would not always be an
expression if it is composed with a filter 'if'. If we extend the usage of
conditional expressions as you propose why not in other expressions too?
[ for(a=A) a+(if (a==0) 1) ]
I transcribe here an example of the benefits of the if-else I had used a few
months <http://forum.openscad.org/List-comprehension-filters-tc16069.html>
ago to argue its need. The following list comprehension filter in the new
sintax:
> if ( A ) ( if( B ) x else if (C) y ) else z
could be expressed in the old one by:
> if ( B || C ) !A ? z : B ? x : y
which is rather cryptic and hard to write. Besides, if we change the first
code eliminating the 'else z' part, the standard code will change to:
> if ( A ) if(B || C) B ? x: y
which is far from an obvious change.
runsun wrote
>> a %2==0? a:
*
>> if (a!=1) a*10
*
>
> I'd like this form better, because it sort of focusing the feature of "if"
> as a filter that discards something, but not "sometimes it discards,
> sometimes it picks it back up when else is added".
I agree that the list comprehension 'if' is not always a filter in the new
syntax. But in your alternative the construct ?: would not always be an
expression if it is composed with a filter 'if'. If we extend the usage of
conditional expressions as you propose why not in other expressions too?
> [ for(a=A) a+(if (a==0) 1) ]
--
View this message in context: http://forum.openscad.org/Any-where-to-find-the-doc-for-lc-each-lc-else-and-lc-for-c-tp18823p18836.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
TV
Tim V. Shaporev
Mon, Oct 31, 2016 2:03 PM
On 10/31/2016 4:03 PM, Ronaldo wrote:
if ( A ) ( if( B ) x else if (C) y ) else z
I believe such syntax should be forbidden because it is not obvious what
is else part of if (C).
Do you mean
A ? (B ? x : (C ? y : undef)) : z
could be expressed in the old one by:
if ( B || C ) !A ? z : B ? x : y
Oh, no, it's something different.
I am quite doubtful if this is really benefit.
Just my $0.02
Tim
On 10/31/2016 4:03 PM, Ronaldo wrote:
> I transcribe here an example of the benefits of the if-else I had used a few
> months <http://forum.openscad.org/List-comprehension-filters-tc16069.html>
> ago to argue its need. The following list comprehension filter in the new
> sintax:
>
>> if ( A ) ( if( B ) x else if (C) y ) else z
I believe such syntax should be forbidden because it is not obvious what
is else part of if (C).
Do you mean
A ? (B ? x : (C ? y : undef)) : z
> could be expressed in the old one by:
>> if ( B || C ) !A ? z : B ? x : y
Oh, no, it's something different.
I am quite doubtful if this is really benefit.
Just my $0.02
Tim
R
runsun
Mon, Oct 31, 2016 2:32 PM
The following list comprehension filter in the new sintax:
if ( A ) ( if( B ) x else if (C) y ) else z
could be expressed in the old one by:
if ( B || C ) !A ? z : B ? x : y
which is rather cryptic and hard to write.
Besides, if we change the first code eliminating the 'else z' part, the
standard code will change to:
if ( A ) if(B || C) B ? x: y
which is far from an obvious change.
I think you made it too complicated. It should be expressed as follows
instead :
which, in my opinion, is a lot easier to both read and write than the 3
forms you presented.
Note that I highlight the if(C)y part to emphasize that "an if w/o else
is a filter", and "an if with an else is the same as cond?A:B".
runsun wrote
why not in other expressions too?
[ for(a=A) a+(if (a==0) 1) ]
Because it can be easily achieved with current stable release:
$ Runsun Pan, PhD $ libs: doctest , faces ( git ), offline doc ( git ), runscad.py ( 2 , git ), synwrite ( 2 ); $ tips: hash ( 2 ), matrix ( 2 , 3 ), sweep ( 2 , 3 ), var ( 2 ), lerp , animation ( gif , prodVid , animlib ), precision ( 2 ), xl-control , type , rounded polygon , chfont , tailRecur ( 2, 3 ), isosphere ( 2 ), area , vol/center , RGB , CurvedImg , tests ( 2 ); $ Apps: rollApp , blockscad , openjscad , on AWS ( pdf )
View this message in context: http://forum.openscad.org/Any-where-to-find-the-doc-for-lc-each-lc-else-and-lc-for-c-tp18823p18840.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
Ronaldo wrote
> The following list comprehension filter in the new sintax:
>> if ( A ) ( if( B ) x else if (C) y ) else z
> could be expressed in the old one by:
>> if ( B || C ) !A ? z : B ? x : y
> which is rather cryptic and hard to write.
>
> Besides, if we change the first code eliminating the 'else z' part, the
> standard code will change to:
>> if ( A ) if(B || C) B ? x: y
> which is far from an obvious change.
I think you made it too complicated. It should be expressed as follows
instead :
> A? (B?x:
*
> if(C)y
*
> ) : z
which, in my opinion, is a lot easier to both read and write than the 3
forms you presented.
Note that I highlight the *if(C)y* part to emphasize that "*an if w/o else
is a filter*", and "*an if with an else is the same as cond?A:B*".
runsun wrote
>> a %2==0? a:
*
>> if (a!=1) a*10
*
>
> why not in other expressions too?
>> [ for(a=A) a+(if (a==0) 1) ]
Because it can be easily achieved with current stable release:
> [ for(a=A) a==0? a+1:a ]
-----
$ Runsun Pan, PhD $ libs: doctest , faces ( git ), offline doc ( git ), runscad.py ( 2 , git ), synwrite ( 2 ); $ tips: hash ( 2 ), matrix ( 2 , 3 ), sweep ( 2 , 3 ), var ( 2 ), lerp , animation ( gif , prodVid , animlib ), precision ( 2 ), xl-control , type , rounded polygon , chfont , tailRecur ( 2, 3 ), isosphere ( 2 ), area , vol/center , RGB , CurvedImg , tests ( 2 ); $ Apps: rollApp , blockscad , openjscad , on AWS ( pdf )
--
View this message in context: http://forum.openscad.org/Any-where-to-find-the-doc-for-lc-each-lc-else-and-lc-for-c-tp18823p18840.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
R
runsun
Mon, Oct 31, 2016 2:43 PM
Tim, I had the similar confusion, took me a lot of "brain twists" to
understand it. Now that after I keep talking to myself for hours:
"an if w/o else is a filter", and "an if with an else is the same as
cond?A:B".
I am able to decode it and I think once you keep reminding yourself this
principle, it'd be easier for you, too.
So the example:
if ( A ) ( if( B ) x else if (C) y ) else z
has 3 if's: the first and 2nd ones, if(A) and if(B), are followed by an
"else", so are the same as A?~:~ and B?~:~, respectively
The 3rd one, if(C), has no "else", so it is a filter. So it can be
translated to the "imagined workable code":
A? ( B?x: if(C)y) : z
Once this principle is embedded in mind, it becomes easy.
In the mean time, however, I believe this construct will continue to
frustrate future (or even current) users.
$ Runsun Pan, PhD $ libs: doctest , faces ( git ), offline doc ( git ), runscad.py ( 2 , git ), synwrite ( 2 ); $ tips: hash ( 2 ), matrix ( 2 , 3 ), sweep ( 2 , 3 ), var ( 2 ), lerp , animation ( gif , prodVid , animlib ), precision ( 2 ), xl-control , type , rounded polygon , chfont , tailRecur ( 2, 3 ), isosphere ( 2 ), area , vol/center , RGB , CurvedImg , tests ( 2 ); $ Apps: rollApp , blockscad , openjscad , on AWS ( pdf )
View this message in context: http://forum.openscad.org/Any-where-to-find-the-doc-for-lc-each-lc-else-and-lc-for-c-tp18823p18841.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
Tim, I had the similar confusion, took me a lot of "brain twists" to
understand it. Now that after I keep talking to myself for hours:
"*an if w/o else is a filter*", and "*an if with an else is the same as
cond?A:B*".
I am able to decode it and I think once you keep reminding yourself this
principle, it'd be easier for you, too.
So the example:
>> if ( A ) ( if( B ) x else if (C) y ) else z
has 3 if's: the first and 2nd ones, if(A) and if(B), are followed by an
"else", so are the same as A?~:~ and B?~:~, respectively
The 3rd one, if(C), has no "else", so it is a filter. So it can be
translated to the "imagined workable code":
A? ( B?x: if(C)y) : z
Once this principle is embedded in mind, it becomes easy.
In the mean time, however, I believe this construct will continue to
frustrate future (or even current) users.
-----
$ Runsun Pan, PhD $ libs: doctest , faces ( git ), offline doc ( git ), runscad.py ( 2 , git ), synwrite ( 2 ); $ tips: hash ( 2 ), matrix ( 2 , 3 ), sweep ( 2 , 3 ), var ( 2 ), lerp , animation ( gif , prodVid , animlib ), precision ( 2 ), xl-control , type , rounded polygon , chfont , tailRecur ( 2, 3 ), isosphere ( 2 ), area , vol/center , RGB , CurvedImg , tests ( 2 ); $ Apps: rollApp , blockscad , openjscad , on AWS ( pdf )
--
View this message in context: http://forum.openscad.org/Any-where-to-find-the-doc-for-lc-each-lc-else-and-lc-for-c-tp18823p18841.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
R
Ronaldo
Mon, Oct 31, 2016 3:35 PM
I think you made it too complicated. It should be expressed as follows
instead :
which, in my opinion, is a lot easier to both read and write than the 3
forms you presented.
We are discussing two different matters: the benefits of the if-else filter
in list comprehension and your alternative sintax of it. The first point of
my last message was about the benefits. So, your recoding above mix the two
things and did not help.
The second issue (your alternative to if-else filter) has it own drawbacks.
With my trivial example:
[ for(a=A) a+(if (a==0) 1) ]
[ for(a=A) if (a==0) a+1 ]
which is a filter, and not your code:
runsun wrote
> I think you made it too complicated. It should be expressed as follows
> instead :
>> A? (B?x:
*
>> if(C)y
*
>> ) : z
> which, in my opinion, is a lot easier to both read and write than the 3
> forms you presented.
We are discussing two different matters: the benefits of the if-else filter
in list comprehension and your alternative sintax of it. The first point of
my last message was about the benefits. So, your recoding above mix the two
things and did not help.
The second issue (your alternative to if-else filter) has it own drawbacks.
With my trivial example:
> [ for(a=A) a+(if (a==0) 1) ]
I meant:
> [ for(a=A) if (a==0) a+1 ]
which is a filter, and not your code:
> [ for(a=A) a==0? a+1:a ]
which is not. The /if/ in the first expression imposes a filter as in your
alternative sintax. That is, if a filter /if/ is allowed as a filter in
conditional expression why wouldn't it be allowed in any expression (keeping
its effect of filtering) ?
--
View this message in context: http://forum.openscad.org/Any-where-to-find-the-doc-for-lc-each-lc-else-and-lc-for-c-tp18823p18843.html
Sent from the OpenSCAD mailing list archive at Nabble.com.