On Saturday, 14 March 2020 at 23:39:11 UTC, Adnan wrote:
In the following code the compiler says the type is Array!()(string):

                        if (key in wordTable)
                                wordTable[key] ~= word;
                        else
                                wordTable[key] = Array!string(word);


source/app.d(29,36): Error: template std.container.array.Array!string.Array.__ctor cannot deduce functi
on from argument types !()(string), candidates are:
/snap/dmd/99/bin/../import/phobos/std/container/array.d(467,5):
     __ctor(U)(U[] values...)
  with U = immutable(char)
  must satisfy the following constraint:
       isImplicitlyConvertible!(U, T)
/snap/dmd/99/bin/../import/phobos/std/container/array.d(501,5):
     __ctor(Range)(Range r)
  with Range = string
  must satisfy the following constraint:
       isImplicitlyConvertible!(ElementType!Range, T)

Full code
string smallestRepr(const string arg) {
        auto repeated = arg ~ arg;
        string result = arg;
        foreach (i; 1 .. arg.length) {
                const slice = repeated[i .. i + arg.length];
                if (slice < result)
                        result = slice;
        }
        return result;
}

unittest {
        assert("cba".smallestRepr() == "acb");
}

void main(const string[] args) {
        import std.stdio : write, File;
        import std.container : Array;

        Array!string[string] wordTable;

        foreach (string word; File(args[1]).byLineCopy()) {
                word = word[0 .. $ - 1]; // strip the newline character
                const string key = word.smallestRepr();
                debug {
                        if (key in wordTable)
                                wordTable[key] ~= word;
                        else
                                wordTable[key] = Array!string(word);
                }
                else {
                        wordTable.require(key, Array!string()) ~= word;
                }
        }

        foreach (array; wordTable.values) {
                if (array.length == 4) {
                        foreach (word; array) {
                                write(word, ", ");
                        }
                        write("\n");
                        break;
                }
        }
}

Reply via email to