January 08, 2015 Re: Why do the same work about 'IndexOfAny' and 'indexOf' function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to FrankLike | use canFind like such: bool a = canFind(strs,s) >= 1; let the compiler figger out what the types of the parameter are. |
January 09, 2015 Re: Why do the same work about 'IndexOfAny' and 'indexOf' function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert burner Schadek | 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 |
January 09, 2015 Re: Why do the same work about 'IndexOfAny' and 'indexOf' function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to FrankLike Attachments: | On Fri, 09 Jan 2015 07:10:14 +0000 FrankLike via Digitalmars-d-learn <digitalmars-d-learn@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! ;-) |
January 09, 2015 Re: Why do the same work about 'IndexOfAny' and 'indexOf' function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | iday, 9 January 2015 at 07:41:07 UTC, ketmar via Digitalmars-d-learn wrote: > On Fri, 09 Jan 2015 07:10:14 +0000 > FrankLike via Digitalmars-d-learn <digitalmars-d-learn@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! ;-) Sorry,it's only a example .Thank you work hard,but it's not what I want. 'indexOfAny ' function should do this work. ”he is at home" ,["home","office",”sea","plane"], in C#,IndexOfAny can do it,what about in D? I know findAmong can do it,but use two function . Thank you. |
January 09, 2015 Re: Why do the same work about 'IndexOfAny' and 'indexOf' function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to FrankLike Attachments: | On Fri, 09 Jan 2015 09:36:01 +0000 FrankLike via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > Sorry,it's only a example .Thank you work hard,but it's > not what I want. > 'indexOfAny ' function should do this work. > ”he is at home" ,["home","office",”sea","plane"], in > C#,IndexOfAny can do it,what about in D? > I know findAmong can do it,but use two function . > Thank you. be creative! ;-) import std.algorithm, std.stdio; void main () { string s = "he is at plane"; if (findAmong!((string a, string b) => b.canFind(a))([s], ["home", "office", "sea", "plane"]).length) { writeln("got it!"); } else { writeln("alas..."); } } or: import std.algorithm, std.stdio; void main () { string s = "he is at home"; if (["home", "office", "sea", "plane"].canFind!((a, string b) => b.canFind(a))(s)) { writeln("got it!"); } else { writeln("alas..."); } } |
January 09, 2015 Re: Why do the same work about 'IndexOfAny' and 'indexOf' function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar |
> be creative! ;-)
>
> import std.algorithm, std.stdio;
>
> void main () {
> string s = "he is at plane";
> if (findAmong!((string a, string b) => b.canFind(a))([s], ["home", "office", "sea", "plane"]).length) {
> writeln("got it!");
> } else {
> writeln("alas...");
> }
> }
>
> or:
>
> import std.algorithm, std.stdio;
>
> void main () {
> string s = "he is at home";
> if (["home", "office", "sea", "plane"].canFind!((a, string b) => b.canFind(a))(s)) {
> writeln("got it!");
> } else {
> writeln("alas...");
> }
> }
The code is the best,and it's better than indexOfAny in C#:
import std.algorithm, std.stdio;
void main ()
{
auto places = [ "home", "office", "sea","plane"];
auto strWhere = "He is in the sea.";
auto where = places.canFind!(a => strWhere.canFind(a));
writeln("Result is ",where);
}
|
January 09, 2015 Re: Why do the same work about 'IndexOfAny' and 'indexOf' function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to FrankLike Attachments: | On Fri, 09 Jan 2015 12:46:53 +0000 FrankLike via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > The code is the best,and it's better than indexOfAny in C#: > > import std.algorithm, std.stdio; > void main () > { > auto places = [ "home", "office", "sea","plane"]; > auto strWhere = "He is in the sea."; > auto where = places.canFind!(a => strWhere.canFind(a)); > writeln("Result is ",where); > } this does unnecessary upvalue access (`strWhere`). try to avoid such stuff whenever it is possible. |
January 09, 2015 Re: Why do the same work about 'IndexOfAny' and 'indexOf' function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | On Friday, 9 January 2015 at 10:02:53 UTC, ketmar via Digitalmars-d-learn wrote: > import std.algorithm, std.stdio; > > void main () { > string s = "he is at home"; > if (["home", "office", "sea", "plane"].canFind!((a, string b) => b.canFind(a))(s)) { > writeln("got it!"); > } else { > writeln("alas..."); > } > } Thank you. The code is the best,and it's better than indexOfAny in C#: /* places.canFind!(a => strWhere.canFind(a)); */ By auto r = benchmark!(f0,f1, f2, f3,f4)(10_0000); Result is : filter is 42ms 85us findAmong is 37ms 268us foreach indexOf is 37ms 841us canFind is 13ms canFind indexOf is 39ms 455us -----------------------5 functions-------------------------- import std.stdio, std.algorithm,std.string; auto places = [ "home", "office", "sea","plane"]; auto strWhere = "He is in the sea."; void main() { auto where = places.filter!(a => strWhere.indexOf(a) != -1); writeln("0 Result is ",where); auto where1 = findAmong(places,strWhere); writeln("1 Result is ",where1); string where2; foreach(a;places) { if(strWhere.indexOf(a) !=-1) { where2 = a; break; } } writeln("2 Result is ",where2); auto where3 = places.canFind!(a => strWhere.canFind(a)); writeln("3 Result is ",where3); auto where4 = places.canFind!(a => strWhere.indexOf(a) != -1); writeln("4 Result is ",where4); } Frank |
January 09, 2015 Re: Why do the same work about 'IndexOfAny' and 'indexOf' function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to FrankLike Attachments: | On Fri, 09 Jan 2015 13:06:09 +0000 FrankLike via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > On Friday, 9 January 2015 at 10:02:53 UTC, ketmar via Digitalmars-d-learn wrote: > > > import std.algorithm, std.stdio; > > > > void main () { > > string s = "he is at home"; > > if (["home", "office", "sea", "plane"].canFind!((a, string > > b) => b.canFind(a))(s)) { > > writeln("got it!"); > > } else { > > writeln("alas..."); > > } > > } > > Thank you. > > The code is the best,and it's better than indexOfAny in C#: > > /* places.canFind!(a => strWhere.canFind(a)); */ > > By auto r = benchmark!(f0,f1, f2, f3,f4)(10_0000); > > Result is : > filter is 42ms 85us > findAmong is 37ms 268us > foreach indexOf is 37ms 841us > canFind is 13ms > canFind indexOf is 39ms 455us > > -----------------------5 functions-------------------------- > import std.stdio, std.algorithm,std.string; > > auto places = [ "home", "office", "sea","plane"]; > auto strWhere = "He is in the sea."; > > void main() > { > auto where = places.filter!(a => strWhere.indexOf(a) != -1); > writeln("0 Result is ",where); > > auto where1 = findAmong(places,strWhere); > writeln("1 Result is ",where1); > > string where2; > foreach(a;places) > { > if(strWhere.indexOf(a) !=-1) > { > where2 = a; > break; > } > } > writeln("2 Result is ",where2); > > auto where3 = places.canFind!(a => strWhere.canFind(a)); > writeln("3 Result is ",where3); > > auto where4 = places.canFind!(a => strWhere.indexOf(a) != -1); > writeln("4 Result is ",where4); > } > > Frank if you *really* concerned with speed here, you'd better consider using regular expressions. as regular expression can be precompiled and then search for multiple words with only one pass over the source string. i believe that std.regex will use variation of Thomson algorithm for regular expressions when it is able to do so. |
January 09, 2015 Re: Why do the same work about 'IndexOfAny' and 'indexOf' function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | On Friday, 9 January 2015 at 13:25:17 UTC, ketmar via Digitalmars-d-learn wrote:
> if you *really* concerned with speed here, you'd better consider using
> regular expressions. as regular expression can be precompiled and then
> search for multiple words with only one pass over the source string. i
> believe that std.regex will use variation of Thomson algorithm for
> regular expressions when it is able to do so.
IMO that is not sound advice. Creating the state machine and running will be more costly than using canFind or indexOf how basically only compare char by char.
If speed is really need use strstr and look if it uses sse to compare multiple chars at a time. Anyway benchmark and then benchmark some more.
|
Copyright © 1999-2021 by the D Language Foundation