Random string samples & unicode

bearophile bearophileHUGS at lycos.com
Sat Sep 11 11:14:23 PDT 2010


> The compiler has full type information, so what's wrong in concatenating two
> char or two dchar into a string or dstring?

But in C the ~ among two chars has a different meaning, so in D you may at best disallow it.


> And I think there are other problems:
> http://d.puremagic.com/issues/show_bug.cgi?id=4853

So that's invalid, I have closed it.

Using a bit of contortions it's possible to write lookAndSay() with no casts, but the code is not good still:


import std.stdio, std.conv, std.algorithm;

string lookAndSay(string input) {
    string result;
    foreach (g; group(input)) {
        string s = to!string(g._1);
        s ~= g._0; // string ~ dchar wrong, string ~= dchar good
        result ~= s;
    }
    return result;
}

void main() {
    string last = "1";
    writeln(last);
    foreach (i; 0 .. 10) {
        last = lookAndSay(last);
        writeln(last);
    }
}

Bye,
bearophile


More information about the Digitalmars-d mailing list