Hi all
I am trying to create a cartesian product generator using macro.
While generating pairs is basically solved, I cannot grasp how to accumulate resulting pairs like following:
[ [1,1], [1,2], [1,3], [2,1] … ]
#![feature(trace_macros)]
trace_macros!(true);
macro_rules! iter {
( $submacro:tt, $each:tt, $arg:tt, [] ) => {
};
( $submacro:tt, $each:tt, $arg:tt, [ $head:tt $(, $tail:tt )* ] ) => {
$each!($submacro, $head, $arg);
iter!($submacro, $each, $arg, [ $($tail),* ]);
};
}
macro_rules! cartesian {
( $submacro:tt, [ $head1:tt $(, $tail1:tt )* ], [ $head2:tt $(, $tail2:tt )* ]) => {
iter!($submacro, cartesian, [ $head2, $($tail2),* ], [ $head1, $($tail1),* ] )
};
( $submacro:tt, [ $head:tt $(, $tail:tt )* ] ) => {
// multiply arr by arr
cartesian( $submacro, [ $head, $($tail),* ], [ $head, $($tail),* ]);
};
( $submacro:tt, $item1:tt, [ $head:tt $(, $tail:tt )* ] ) => {
iter!($submacro, cartesian, $item1, [ $head, $($tail),* ])
};
( $submacro:tt, $item1:tt, $item2:tt ) => {
// getting pairs
$submacro!($item1, $item2)
};
}
macro_rules! test {
($e1:tt, $e2:tt) => {
// getting pairs here
println!("[{:?} {:?}]", $e1, $e2);
}
}
cartesian!(test, [1,2,3,4], [1,2,3,4]);
However I cannot accumulate those pairs as list.
And getting list seems to be necessary for generating match arms (both condition and arm expression)
Any hints?