On 11/27/2012 06:45 PM, jerro wrote:
You could replace the inner loop with somehting like:
bool excl = exclude.any!(part => name.canFind(part));
std.algorithm seems to generally be running the match in the opposite
direction, if I'm understanding it properly. (Dealing with D template
is always confusing to me.) OTOH, I couldn't find the string any
method, so I'm not really sure what you're proposing, though it does
look attractive.
I don't understand what you mean with running the match in the opposite
direction, but I'll explain how my line of code works. First of all, it
is equivalent to:
any!(part => canFind(name, part))(exclude);
The feature that that lets you write that in the way I did in my
previous post is called uniform function call syntax (often abbreviated
to UFCS) and is described at
http://www.drdobbs.com/cpp/uniform-function-call-syntax/232700394.
canFind(name, part) returns true if name contains part.
(part => canFind(name, part)) is a short syntax for (part){ return
canFind(name, part); }
any!(condition)(range) returns true if condition is true for any element
of range
So the line of code in my previous post sets excl to true if name
contains any of the strings in exclude. If you know all the strings you
want to exclude in advance, it is easier to do that with a regex like
Joshua did.
If you want to learn about D templates, try this tutorial:
https://github.com/PhilippeSigaud/D-templates-tutorial/blob/master/dtemplates.pdf?raw=true
Still, though your basic approach sounds good, the suggestion of
Joshua Niehus would let me filter out the strings that didn't fit
before entering the loop. There's probably no real advantage to doing
it that way, but it does seem more elegant.
I agree, it is more elegant.
Thanks for the tutorial link, I'll give it a try. (Whee! A 182 page
tutorial!) Those things, though, don't seem to stick in my mind. I
learned programming in FORTRAN IV, and I don't seem to be able to force
either templates, Scheme, or Haskell into my way of thinking about
programming. (Interestingly, classes and structured programming fit
without problems.)
The link to the Walter article in Dr. Dobbs is interesting. I intend to
read it first.
OTOH, I still don't know where "any" is documented. It's clearly some
sort of template instantiation, but it doesn't seem to be defined in
either std.string or std.object (or anywhere else I've thought to
check). And it look as if it would be something very useful to know.