February 05, 2012 Re: [your code here] | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Manfred Nowak | On 2/4/12 10:05 PM, Manfred Nowak wrote:
> Jos van Uden wrote:
>
>> bool isPangram
>
> The presented code is not an acceptable example for the usage of the D
> programming language.
>
> 1)
> `indexOf( s, c)' has a worst case running time of O( `s.length').
> `indexOf' is called once for each `c' in the used member `alpha' of
> `Alphabet'.
> Therefore the runtime of the presented code is
> O( `s.length' * `alpha.length')
> whereas
> O( `s.length' + `alpha.length')
> is possible.
>
> 2)
> The optional third parameter of `indexOf' can be called with
> `CaseSensitive.no'. But that parameter is left untouched. Instead a
> check with `toUpper( c)' is used, thereby risking a further visitation
> of the whole string .
>
> 3)
> The use of the literal value `-1' stands out of the code and hints to a
> maldesign in phobos. This is because `-1' is not mnemonic.
Sensible arguments. Would you want to redo the example in better style?
Andrei
| |||
February 05, 2012 Re: [your code here] | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Manfred Nowak | On 5-2-2012 5:05, Manfred Nowak wrote:
> 2)
> The optional third parameter of `indexOf' can be called with
> `CaseSensitive.no'. But that parameter is left untouched. Instead a
> check with `toUpper( c)' is used, thereby risking a further visitation
> of the whole string .
Using CaseSensitive.no is a lot slower
| |||
February 06, 2012 Re: [your code here] | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jos van Uden | >> The optional third parameter of `indexOf' can be called with >> `CaseSensitive.no'. But that parameter is left untouched. Instead a check with `toUpper( c)' is used, thereby risking a further visitation of the whole string . > > Using CaseSensitive.no is a lot slower Hmmm then perhaps a whole re-write. I've gotten this done, although I'll need someone to tell me how well it does. Far as I can tell it's O(n). It's based loosely on the ascii version, except templatized. Hope it's not too ugly... import std.stdio, std.string, std.traits, std.uni, std.conv; string T_isPangram(string name, string set, string uset = "") { string x = "bool " ~ name ~ "(S)(S s) if (isSomeString!S) {\n"; if(set.length > 32) { x ~= "\tulong bitset;\n"; } else { x ~= "\tuint bitset;\n"; } x ~= "\tforeach(dchar c; s) {\n\t\tswitch(toLower(c)) {\n"; foreach(i, c; set) { x ~= "\t\t\tcase '" ~ c ~ "':"; x ~= "\tbitset |= 1 << " ~ to!string(i) ~ "; break;\n"; } int i = set.length; while(uset.length >= 6) { x ~= "\t\t\tcase '" ~ uset[0 .. 6] ~ "':"; x ~= "\tbitset |= 1 << " ~ to!string(i) ~ "; break;\n"; uset = uset[6 .. $]; i++; } x ~= "\t\t\tdefault:\n\t\t}\n\t}\n\treturn bitset == (1 << " ~ to!string(i) ~ ") - 1;\n}"; return x; } mixin(T_isPangram("EN_isPangram", "abcdefghijklmnopqrstuvwxyz")); mixin(T_isPangram("DE_isPangram", "abcdefghijklmnopqrstuvwxyz", "\\u00DF\\u00e4\\u00f6\\u00dc")); mixin(T_isPangram("SV_isPangram", "abcdefghijklmnopqrstuvwxyz", "\\u00e5\\u00e4\\u00f6")); unittest { writeln(T_isPangram("TEST_isPangram", "ab", "\\u00DF\\u00e4")); //example output below assert(!EN_isPangram("this doesn't cover everything")); assert(EN_isPangram("the quick brown fox jumps over the LAZY dog")); //to-lower check along with english assert(DE_isPangram("Falsches Üben von Xylophonmusik quält jeden größeren Zwerg")); assert(SV_isPangram("Yxskaftbud, ge vår wczonmö iqhjälp"w)); } /* bool TEST_isPangram(S)(S s) if (isSomeString!S) { uint bitset; foreach(dchar c; s) { switch(toLower(c)) { case 'a': bitset |= 1 << 0; break; case 'b': bitset |= 1 << 1; break; case '\u00DF': bitset |= 1 << 2; break; case '\u00e4': bitset |= 1 << 3; break; default: } } return bitset == (1 << 4) - 1; } */ | |||
February 06, 2012 Re: [your code here] | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Era Scarecrow | > mixin(T_isPangram("DE_isPangram", "abcdefghijklmnopqrstuvwxyz", "\\u00DF\\u00e4\\u00f6\\u00dc")); somehow this got mixed up with the earlier messed up one.. this is the correction for this line. mixin(T_isPangram("DE_isPangram", "abcdefghijklmnopqrstuvwxyz", "\\u00DF\\u00e4\\u00f6\\u00fc")); | |||
February 06, 2012 Re: [your code here] | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Era Scarecrow | Am 06.02.2012, 07:38 Uhr, schrieb Era Scarecrow <rtcvb32@yahoo.com>:
>> mixin(T_isPangram("DE_isPangram", "abcdefghijklmnopqrstuvwxyz", "\\u00DF\\u00e4\\u00f6\\u00dc"));
>
> somehow this got mixed up with the earlier messed up one.. this is the correction for this line.
>
> mixin(T_isPangram("DE_isPangram", "abcdefghijklmnopqrstuvwxyz", "\\u00DF\\u00e4\\u00f6\\u00fc"));
Actually, yeah a bit Scarecrow like. For dlang or rosettacode I prefer short and easy to read code. The one example where the result is checked against 0b11_1111111_11111111_11111111 was really nice, since it expresses the bit set nature in a concise and readable fashion. But it also shows that you can put integer literals in dual notation and place _ to segment the number.
| |||
February 06, 2012 Re: [your code here] | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | Andrei Alexandrescu wrote:
> Would you want to redo the example
No.
I do not see the usefullness of a random ten liner shown on the frontpage of a programming language.
If the goal is to attract the attention of a visitor to some feature of the language, that feature should be stated prominently and the example code should be shown on request.
At least such a goal is not obvious for this example.
-manfred
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply