Better idea for double list comprehension?
CJS
Prometheus85 at hotmail.com
Fri Jan 17 21:42:57 PST 2014
Hit the wrong key and posted too early. I finished the code
sample below. My main question was for something prettier and
more concise. I feel like the code below is long and not as
pretty in comparison to the Python. Sometimes that's an
unavoidable consequence of static typing, but I'm not sure that's
the case here.
On Saturday, 18 January 2014 at 05:40:56 UTC, CJS wrote:
> 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]));
}
return grid;
}
More information about the Digitalmars-d-learn
mailing list