www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - echo: "-n", the next installment

reply "John Colvin" <john.loughran.colvin gmail.com> writes:
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
Jul 25 2013
next sibling parent reply "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
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.
Jul 26 2013
parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
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.
Jul 27 2013
next sibling parent "Joseph Rushton Wakeling" <joseph.wakeling webdrake.net> writes:
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 ).
Jul 27 2013
prev sibling parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
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 :/
Jul 27 2013
parent "John Colvin" <john.loughran.colvin gmail.com> writes:
On Saturday, 27 July 2013 at 12:59:06 UTC, monarch_dodra wrote:
 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 :/
Also, this wouldn't work $ echo -E fdsa -n fdsa -n $ as getopt would parse the -n as an option. Exact replication is the name of the game here, as often in the real world one has to write to a spec that is subtly different to what library writers had in mind.
Jul 27 2013
prev sibling parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
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: In regards to the whole "spaces only *between*" elements issue, it is always good to know that writef knows how to print a range, and knows how to add a "separator" tokens. This make it trivailly easy to print a range of elements, adding tokens *only* between each elements. I know it's not necessarily what you want to teach, but it *is* incredibly useful. Your first example becomes the trivial: import std.stdio : writefln; void main(string[] args) { assert(args.length); args = args[1 .. $]; debug writefln("[%(%s, %)]", args); else writefln("%-(%s %)", args); } when invoked with "a b c", it prints: a b c If compiled with "-debug" to boot, you can add extra "visual" tokens to check there are no trailing symbols: ["a", "b", "c"] See? Who needs a foreach, or a loop, or functional programming ;)
Jul 27 2013
parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
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 :)
Jul 27 2013
parent "John Colvin" <john.loughran.colvin gmail.com> writes:
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.
Jul 27 2013