May 11, 2016
On Wednesday, 11 May 2016 at 13:29:56 UTC, Gopan wrote:
> On Tuesday, 10 May 2016 at 09:52:07 UTC, Mathias Lang wrote:
>
>>
>> Do you like comma expressions, ...
>
> I am a student.
> In C, one scenario where I like comma is this.
>
>
> int x;
> while( scanf("%d", &x),  x!= 0) // until user input 0.
> {
>    //do something with x
> }
>
> Without the comma operator, I would have to repeat the scanf statement.
> int x;
> scanf("%d", &x);
> while(x != 0)
> {
>    //do something with x
>    scanf("%d", &x);
> }
>
> Does anybody think that this is a useful case of comma operator?

What if scanf fails or hits eof?

A better call:

while(scanf("%d", &x) > 0 && x != 0)

-Steve
May 11, 2016
On Wednesday, 11 May 2016 at 13:29:56 UTC, Gopan wrote:
> int x;
> while( scanf("%d", &x),  x!= 0) // until user input 0.
> {
>    //do something with x
> }
>
> Does anybody think that this is a useful case of comma operator?

Well, it is, but judging from my experience, comma operator is the most rarely used part of the language. I hardly ever needed it in C and C++, where it originated, same with D and Java. In fact, I had to check if it works in Java at all - turned out, it's only allowed in "for" loops. Just never used it and forgot about it.

The comma operator does more harm than good, is very rarely used and is far less usable than tuples, so I'm all for killing it (except in "for" loops).
May 11, 2016
On Wed, May 11, 2016 at 02:37:37PM +0000, burjui via Digitalmars-d wrote:
> On Wednesday, 11 May 2016 at 13:29:56 UTC, Gopan wrote:
> >int x;
> >while( scanf("%d", &x),  x!= 0) // until user input 0.
> >{
> >   //do something with x
> >}
> >
> >Does anybody think that this is a useful case of comma operator?
> 
> Well, it is, but judging from my experience, comma operator is the most rarely used part of the language. I hardly ever needed it in C and C++, where it originated, same with D and Java. In fact, I had to check if it works in Java at all - turned out, it's only allowed in "for" loops. Just never used it and forgot about it.
> 
> The comma operator does more harm than good, is very rarely used and is far less usable than tuples, so I'm all for killing it (except in "for" loops).

That's what I've been saying, it should be treated as a special case in the syntax of for-loops, but not as an operator in general.


T

-- 
A computer doesn't mind if its programs are put to purposes that don't match their names. -- D. Knuth
May 11, 2016
On Wednesday, 11 May 2016 at 13:29:56 UTC, Gopan wrote:
> int x;
> while( scanf("%d", &x),  x!= 0) // until user input 0.
> {
>    //do something with x
> }
>
> Without the comma operator, I would have to repeat the scanf statement.
> int x;
> scanf("%d", &x);
> while(x != 0)
> {
>    //do something with x
>    scanf("%d", &x);
> }

Aside from scanf specifics, you shouldn't repeat the setup code, use do...while(true) with if and break.

In places where the comma operator does help,  use a comma(expr,result) template function, implemented here:
http://forum.dlang.org/post/ngslcl$otg$1@digitalmars.com
May 11, 2016
On Wed, May 11, 2016 at 04:46:48PM +0000, Nick Treleaven via Digitalmars-d wrote:
> On Wednesday, 11 May 2016 at 13:29:56 UTC, Gopan wrote:
> >int x;
> >while( scanf("%d", &x),  x!= 0) // until user input 0.
> >{
> >   //do something with x
> >}
> >
> >Without the comma operator, I would have to repeat the scanf
> >statement.
> >int x;
> >scanf("%d", &x);
> >while(x != 0)
> >{
> >   //do something with x
> >   scanf("%d", &x);
> >}
> 
> Aside from scanf specifics, you shouldn't repeat the setup code, use do...while(true) with if and break.
[...]

Actually, the 2nd way he wrote it above is my preferred way, because it reveals the correspondence between the structure of the code and the structure of the data better than any of the various shortcuts people like to write.

But a far superior way is to encapsulate reading input as a range (or equivalent data source) and completely separate the data source from the data processing:

	auto scanInts() {
		struct Range {
			bool empty = true;
			int front;
			void popFront() {
				if (scanf("%d", &front) > 0)
					empty = false;
			}
		}
		Range r;
		r.popFront();
		return r;
	}

	foreach (x; ScanfRange().until!(x => x != 0)) {
		// do something with x
	}

This way your data processing code doesn't ever have to worry about the dirty details of how to loop over scanf calls, and can easily be hooked into a different data source with minimal changes.


T

-- 
I am Ohm of Borg. Resistance is voltage over current.
May 11, 2016
On Wednesday, 11 May 2016 at 16:46:48 UTC, Nick Treleaven wrote:
> In places where the comma operator does help,  use a comma(expr,result) template function, implemented here:
> http://forum.dlang.org/post/ngslcl$otg$1@digitalmars.com

May not always work: https://dpaste.dzfl.pl/1ea0df70787b
May 11, 2016
Eeek, dpaste swallowed error message :)
May 11, 2016
https://dpaste.dzfl.pl/46f24c3def62
May 11, 2016
On Wednesday, 11 May 2016 at 16:44:43 UTC, H. S. Teoh wrote:
> That's what I've been saying, it should be treated as a special case in the syntax of for-loops, but not as an operator in general.
>

Please no special cases.

May 11, 2016
On Wednesday, 11 May 2016 at 17:00:41 UTC, H. S. Teoh wrote:
> On Wed, May 11, 2016 at 04:46:48PM +0000, Nick Treleaven via Digitalmars-d wrote:
>> On Wednesday, 11 May 2016 at 13:29:56 UTC, Gopan wrote:
>> >int x;
>> >while( scanf("%d", &x),  x!= 0) // until user input 0.
>> >{
>> >   //do something with x
>> >}
>> >
>> >Without the comma operator, I would have to repeat the scanf
>> >statement.
>> >int x;
>> >scanf("%d", &x);
>> >while(x != 0)
>> >{
>> >   //do something with x
>> >   scanf("%d", &x);
>> >}
>> 
>> Aside from scanf specifics, you shouldn't repeat the setup code, use do...while(true) with if and break.
> [...]
>
> Actually, the 2nd way he wrote it above is my preferred way, because it reveals the correspondence between the structure of the code and the structure of the data better than any of the various shortcuts people like to write.

The problem with the second way is that the piece of code (scanf) that prepares the condition (x!=0) appears two times.  One before the while-loop and one towards the end of the while-loop block.  In a maintenance project, this duplication is a problem.  Somebody doing a bug fix is probable to do the fix in only one place, missing the other.  That was what I tried to avoid by using comma.  When there are multiple setup stattement, I too prefer the other trick (Nick Treleaven's comment)

do
{
   [ code that sets up condition-expression]

   if(expression)
       break;
}while(true);

> But a far superior way is to encapsulate reading input as a range (or equivalent data source) and completely separate the data source from the data processing:
>
> 	auto scanInts() {
> 		struct Range {
> 			bool empty = true;
> 			int front;
> 			void popFront() {
> 				if (scanf("%d", &front) > 0)
> 					empty = false;
> 			}
> 		}
> 		Range r;
> 		r.popFront();
> 		return r;
> 	}
>
> 	foreach (x; ScanfRange().until!(x => x != 0)) {
> 		// do something with x
> 	}
>
> This way your data processing code doesn't ever have to worry about the dirty details of how to loop over scanf calls, and can easily be hooked into a different data source with minimal changes.
>
>
> T