Bug 9878 – std.algorithm.cartesianProduct results order
Status
RESOLVED
Resolution
FIXED
Severity
minor
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-04-04T19:48:00Z
Last change time
2014-07-10T21:34:28Z
Assigned to
nobody
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2013-04-04T19:48:53Z
This is an example of Python usage of its product() function:
>>> from itertools import product
>>> list(product([0, 1], repeat=3))
[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]
This is a similar usage of Phobos std.algorithm.cartesianProduct:
import std.stdio, std.algorithm, std.string, std.conv, std.array;
void main() {
auto bits = [0, 1];
auto p = cartesianProduct(bits, bits, bits);
p.text.replace("Tuple!(int, int, int)", "").writeln;
}
Its output:
[(0, 0, 0), (1, 0, 0), (0, 1, 0), (1, 1, 0), (0, 0, 1), (1, 0, 1), (0, 1, 1), (1, 1, 1)]
I'd like cartesianProduct() to give its results in the same order as Python. If you see in Python the results are like the binary numbers:
000
001
010
011
100
101
110
111
I think this is a more natural and more useful order.
Comment #3 by bearophile_hugs — 2013-05-28T02:56:25Z
(In reply to comment #2)
> where I suggest to give a template parameter to specify
> lexicographic/antilexicographic, along with depth first/breadth first.
If you want to add an option to specify the ordering, then I'd like the order discussed here (lexicographic) to be the default one, because it's the most commonly needed by me, and it's what I expect when I port Python code to D.