Closed alexchandel closed 9 years ago
An adaptor for product is a good idea, I guess it's the most natural way to use it. Not sure if we can make it expand tuples like the iproduct macro does (((x, y), z) → (x, y, z)
).
That would be preferable, but I think it requires variadic generics, or at minimum having two impls, one for Item=A
and one for Item=(A,B)
.
I'd be satisfied with the basics for now. I can always map(|((x,y),z)| (x,y,z))
until then.
Is there any reason to prefer the long name cartesian_product
? I.e. are there other products we might want to add?
@bluss Can't think of any others (not that there aren't any), but we want to avoid product()
because it's taken by MultiplicativeIterator. times()
would work too, though it's less specific.
Something else very useful is a Cartesian product without a hardcoded length, i.e., concretely something like taking a vector of iterators and returning a iterator of vectors: a form of Haskell's sequenceA
, where
sequenceA [[0..2], [0..2], [0..2]] == [[0,0,0],[0,0,1],[0,0,2],[0,1,0],[0,1,1],[0,1,2],[0,2,0],[0,2,1],[0,2,2],[1,0,0],[1,0,1],[1,0,2],[1,1,0],[1,1,1],[1,1,2],[1,2,0],[1,2,1],[1,2,2],[2,0,0],[2,0,1],[2,0,2],[2,1,0],[2,1,1],[2,1,2],[2,2,0],[2,2,1],[2,2,2]]
In addition to
iproduct!(iter_a, iter_b)
, Itertools should have something likeiter_a.times(iter_b)
oriter_a.cartesian_product(iter_b)
. This would let you take the product of iterators inline, functional style.Maybe a method like this:
This would allow you to do much better inline operations, like:
This shouldn't require higher-kinded types either.