Better idea for double list comprehension?
CJS
Prometheus85 at hotmail.com
Sat Jan 18 18:10:00 PST 2014
>
> to!string(c) ===> c.text
>
That's more concise but I also think it's more confusing. I
assume that to!string is doing the exact same thing, but I was
hoping for something to do the appropriate implicit
conversations. Especially to a range of length 1, though I can
understand that kind of auto-magical conversion would be annoying
in a number of important instances.
I changed the code to this:
import std.stdio : writeln;
import std.range : chunks, chain;
import std.algorithm;
import std.array;
import std.conv;
auto cross(R1, R2)(R1 A, R2 B) {
return cartesianProduct(A, B).map!(ab => ab[].text).array;
}
void main(){
const string letters = "ABCDEFGHI";
const string digits = "123456789";
auto cols = digits;
auto rows = letters;
auto squares = cross(rows, cols);
string[][] unitlist;
foreach(c; cols){
unitlist ~= cross(rows, to!string(c));
}
foreach(r; rows){
unitlist ~= cross(to!string(r), cols);
}
foreach(r; chunks(rows, 3)){
foreach(c; chunks(cols, 3)){
unitlist ~= cross(to!string(r),to!string(c));
}
}
string[][][string] units;
string[][string] peers;
foreach(s; squares){
units[s] = unitlist.filter!(x => any!(y => s==y)); \\line
37
}
foreach(s; squares){
peers[s] = remove(chain(units[s]), s); \\line 41
}
}
On dmd 2.064.2 (downloaded and installed today) on 64-bit linux I
get the following errors:
sudoku.d(37): Error: cannot resolve type for any!((y) => s == y)
/home/cjordan1/dmd2/linux/bin64/../../src/phobos/std/algorithm.d(1381):
Error: template instance sudoku.main.__lambda1!(string[]) error
instantiating
/home/cjordan1/dmd2/linux/bin64/../../src/phobos/std/algorithm.d(1369):
instantiated from here: FilterResult!(__lambda1,
string[][])
sudoku.d(37): instantiated from here: filter!(string[][])
/home/cjordan1/dmd2/linux/bin64/../../src/phobos/std/algorithm.d(1369):
Error: template instance sudoku.main.FilterResult!(__lambda1,
string[][]) error instantiating
sudoku.d(37): instantiated from here: filter!(string[][])
sudoku.d(37): Error: template instance sudoku.main.filter!((x) =>
any!((y) => s == y)).filter!(string[][]) error instantiating
sudoku.d(41): Error: cannot implicitly convert expression
(remove(chain(units[s]), s)) of type string[][] to string[]
Lines 37 and 41 are noted in the above code.
More information about the Digitalmars-d-learn
mailing list