Why do the same work about 'IndexOfAny' and 'indexOf' function?

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jan 8 23:40:57 PST 2015


On Fri, 09 Jan 2015 07:10:14 +0000
FrankLike via Digitalmars-d-learn <digitalmars-d-learn at puremagic.com>
wrote:

> On Thursday, 8 January 2015 at 15:15:59 UTC, Robert burner 
> Schadek wrote:
> >
> > use canFind like such:
> >     bool a = canFind(strs,s) >= 1;
> >
> > let the compiler figger out what the types of the parameter are.
> 
> canFind is work for such as :
>   bool x = canFind(["exe","lib","a","dll"],"a" );
> but can't work for canFind(["exe","lib","a","dll"],"hello.lib");
> 
> So  I very want to let the function 'indexOfAny' do the same work.
> 
> Thank you.
> 
> Frank
be creative! ;-)

  import std.algorithm, std.stdio;

  void main () {
    string fname = "hello.exe";
    import std.path : extension;
    if (findAmong([fname.extension], [".exe", ".lib", ".a", ".dll"]).length) {
      writeln("got it!");
    } else {
      writeln("alas...");
    }
  }

note the dots in extension list.

yet you can do it even easier:

  import std.algorithm, std.stdio;

  void main () {
    string fname = "hello.exe";
    import std.path : extension;
    if ([".exe", ".lib", ".a", ".dll"].canFind(fname.extension)) {
      writeln("got it!");
    } else {
      writeln("alas...");
    }
  }

as you obviously interested in extension here -- check only that
part! ;-)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20150109/22edf4c7/attachment-0001.sig>


More information about the Digitalmars-d-learn mailing list