More general Cartesian product
Magnus Lie Hetland
magnus at hetland.org
Wed Feb 29 06:00:54 PST 2012
Hi!
I have a need for a Cartesian product of multiple ranges. I see there's
been a discussion here (Dec 2011) as well as a request posted (#7128).
It seems to me that the request deals with a multidimensional product
-- which is what I need -- while the implementation by Timon Gehr deals
only with the two-dimensional case.
I guess I could apply it in a nested fashion, but there's still the
issue of flattening the result.
My application is a template along the lines of
void forall(alias func, T...)(T args) {
...
}
which would call func with every combination of parameters taken from
the ranges in args. So that, for example, forall!foo([1, 2], ["a",
"b"]) would yield four calls, from foo(1, "a") to foo(2, "b").
But the thing is that I'd like an arbitrary number of arguments. Sure,
I could set an upper limit and hard-code the cases -- but there must be
a prettier way? I've made some stabs at a recursive version, DMD
complaining all the while. Any pointers?
(Now, I would eventually like to do more complex versions, using only a
*subset* of the Cartesian product (for, e.g., all-pairs testing or
combinatorial testing).)
--
Magnus Lie Hetland
http://hetland.org
More information about the Digitalmars-d-learn
mailing list