Better idea for double list comprehension?
    bearophile 
    bearophileHUGS at lycos.com
       
    Sat Jan 18 03:02:45 PST 2014
    
    
  
CJS:
> 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"]).
One solution:
import std.stdio, std.conv, std.algorithm, std.array;
string[] cross(in string A, in string B) {
     return cartesianProduct(A, B).map!(ab => ab[].text).array;
}
void main() {
     cross("ab", "12").writeln;
}
But note that currently cartesianProduct doesn't return the pairs 
in a natural order.
cross() should be pure.
Bye,
bearophile
    
    
More information about the Digitalmars-d-learn
mailing list