Better idea for double list comprehension?

CJS Prometheus85 at hotmail.com
Fri Jan 17 21:40:54 PST 2014


I'm trying to write a D function that does the same as this 
Python function:

def cross(A, B):
     "Cross product of elements in A and elements in B."
     return [a+b for a in A for b in B]

where A and B are strings. (So cross("ab","12") is ["a1", "b1", 
"a2", "b2"]).

It's easy to get something that works the same in D, but I'd like 
to make it as simple and short as possible. The best I could come 
up with is


string[] cross(string A, string B){
     string[] grid;
     foreach(t; cartesianProduct(A,B)){
         grid ~= (to!string(p[0]) ~ to!string(p[1]));



More information about the Digitalmars-d-learn mailing list