Re: echo: -n, the next installment

2013-07-27 Thread John Colvin

On Saturday, 27 July 2013 at 01:09:03 UTC, Jesse Phillips wrote:

On Friday, 26 July 2013 at 00:38:46 UTC, John Colvin wrote:
After a few weeks of not getting around to it, here's my 
second post:


http://foreach-hour-life.blogspot.co.uk/2013/07/the-first-corner-n-for-echo.html


BTW, std.getopt is a good way to parse arguments. Not sure if 
it is relevant to what you want to teach, but should generally 
be preferred over handwritten.


I'm pretty sure it wouldn't work ideally for echo as the 
behaviour depends on the order of the arguments.


Re: echo: -n, the next installment

2013-07-27 Thread Joseph Rushton Wakeling

On Saturday, 27 July 2013 at 12:19:44 UTC, John Colvin wrote:
I'm pretty sure it wouldn't work ideally for echo as the 
behaviour depends on the order of the arguments.



It also has some odd little niggles -- e.g. it's not nice that 
with a short option you can have --t 5 and --t=5 but not -t 5 or 
-t=5 (according to docs you can only use -t5 ).


Re: echo: -n, the next installment

2013-07-27 Thread monarch_dodra

On Saturday, 27 July 2013 at 12:39:24 UTC, monarch_dodra wrote:

On Friday, 26 July 2013 at 00:38:46 UTC, John Colvin wrote:
After a few weeks of not getting around to it, here's my 
second post:


http://foreach-hour-life.blogspot.co.uk/2013/07/the-first-corner-n-for-echo.html


I tried to post a comment on your blog, but I failed. Anyways, 
I wanted to post:


And here is the second comment I wanted to put:

When parsing the options, you use an if-else. I don't know if its 
just me, but I find that using a switch is clearer (it's what you 
do in your second example). It also introduces string cases 
(illegal in C++), and labeled control statements.


The code becomes:

import std.stdio : writef, writeln;
void main(string[] args)
{
assert(args.length);
args = args[1 .. $];
bool writeNewline = true;

size_t i = 0;
myForeach: foreach(arg; args)
{
switch(arg)
{
case -n:
writeNewline = false;
++i;
break;

default:
break myForeach;
}
}
args = args[i .. $];

writef(%-(%s %), args);
if(writeNewline)
writeln();
}

PS: If I figure out how to comment on your blog, I'll paste the 
comments there :)


Re: echo: -n, the next installment

2013-07-27 Thread monarch_dodra

On Saturday, 27 July 2013 at 12:19:44 UTC, John Colvin wrote:

On Saturday, 27 July 2013 at 01:09:03 UTC, Jesse Phillips wrote:

On Friday, 26 July 2013 at 00:38:46 UTC, John Colvin wrote:
After a few weeks of not getting around to it, here's my 
second post:


http://foreach-hour-life.blogspot.co.uk/2013/07/the-first-corner-n-for-echo.html


BTW, std.getopt is a good way to parse arguments. Not sure if 
it is relevant to what you want to teach, but should generally 
be preferred over handwritten.


I'm pretty sure it wouldn't work ideally for echo as the 
behaviour depends on the order of the arguments.


getopt knows how to handle ordering, it's really just a matter of 
echo's argument parsing rules being different from classic getopt.


For example, echo does not handle -- argument (end of options 
mark), which means it is literally impossible for echo's first 
string argument to be -n.


So for example, while echo -- -n would print -- -n, a getopt 
echo would print -n.


Arguably, this is better behavior, but if the goal is exact 
replication, then it's wrong :/


Re: echo: -n, the next installment

2013-07-27 Thread John Colvin

On Saturday, 27 July 2013 at 12:52:11 UTC, monarch_dodra wrote:

And here is the second comment I wanted to put:

When parsing the options, you use an if-else. I don't know if 
its just me, but I find that using a switch is clearer (it's 
what you do in your second example). It also introduces string 
cases (illegal in C++), and labeled control statements.


The code becomes:

import std.stdio : writef, writeln;
void main(string[] args)
{
assert(args.length);
args = args[1 .. $];
bool writeNewline = true;

size_t i = 0;
myForeach: foreach(arg; args)
{
switch(arg)
{
case -n:
writeNewline = false;
++i;
break;

default:
break myForeach;
}
}
args = args[i .. $];

writef(%-(%s %), args);
if(writeNewline)
writeln();
}

PS: If I figure out how to comment on your blog, I'll paste the 
comments there :)


thanks for the info on writef, I didn't realise it could do that.

About the switch statement: I didn't even know you could do that 
with labels, I'd only ever used them for gotos. I'll consider 
putting that in the next version.


Re: echo: -n, the next installment

2013-07-26 Thread Jesse Phillips

On Friday, 26 July 2013 at 00:38:46 UTC, John Colvin wrote:
After a few weeks of not getting around to it, here's my second 
post:


http://foreach-hour-life.blogspot.co.uk/2013/07/the-first-corner-n-for-echo.html


BTW, std.getopt is a good way to parse arguments. Not sure if it 
is relevant to what you want to teach, but should generally be 
preferred over handwritten.


echo: -n, the next installment

2013-07-25 Thread John Colvin
After a few weeks of not getting around to it, here's my second 
post:


http://foreach-hour-life.blogspot.co.uk/2013/07/the-first-corner-n-for-echo.html