Canonical/Idiomatic in memory files

Russel Winder russel at winder.org.uk
Wed May 29 00:02:01 PDT 2013


On Tue, 2013-05-28 at 22:33 -0700, Walter Bright wrote:
[…]
> Coincidentally, I wrote a wc program a year ago:

I note the one on the D web site could do with being made more
idiomatic. cf. http://dlang.org/wc.html

> -------------------------
> import std.stdio, std.file, std.string, std.array, std.algorithm, std.typecons;
> import lazysplit;

As far as I know currently (which may mean I am very wrong), D imports
import all symbols defined in the module into the current name space.
This is like "star imports" in Python and Java which are now seen as not
the right thing to do. Python's default is to import the namespace not
the symbols in it and many believe this is the right thing to do.

> alias Tuple!(int, "lines", int, "words", int, "chars") Lwc;

I used an int[3] for this, but I like the labelled tuple. Is this really
just a three item dictionary though. Might a dictionary be a better
structure for this?

> void main(string[] args) {
>      writeln("   lines   words   bytes file");
> 
>      auto total = args[1 .. args.length].wctotal();
> 
>      if (args.length > 2)
>          writefln("--------------------------------------\n%8s%8s%8s total",
>              total[0..3]);
> }

To emulate /usr/bin/wc, I dispensed with the - sequence. Less code ;-)

Is there an idiom of when to use 1..args.length and when to use 1..$ ?

> auto wctotal(R)(R args) {
>      Lwc total;
>      foreach (arg; args) {
>          auto t = arg.File().byLine(KeepTerminator.yes).wc();
> 
>          writefln("%8s%8s%8s %s", t[0..3], arg);
> 
>          foreach(i, v; t)
>              total[i] += v;
>      }
>      return total;
> }

Why a template? R is always string? The above does not cope with a
parameter being "-" to indicate use the stdin.

> auto wc(R)(R r) {
>      Lwc t;
>      foreach (line; r) {
>          t.lines += 1;
>          t.words += line.lazySplit().count();
>          t.chars += line.length;
>      }
>      return t;
> }

The body of this function looks almost, but not quite, exactly like
mine :-)

> Just replace "arg.File().byLine(KeepTerminator.yes)" with a string filled with 
> your mocked data.

No that is not acceptable, the code under test must remain unchanged in
order to be tested.

I have been having trouble with UFCS: x.f() has not been compiling I
have had to use f(x). There must be a reason why rdmd and ldc have not
allowed me to use whichever of the forms I want, I will have to
investigate further to create a smaller exemplar of the issue.

-- 
Russel.
=============================================================================
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder at ekiga.net
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel at winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20130529/861dc65a/attachment.pgp>


More information about the Digitalmars-d mailing list