*Not to be confused with having anything to do with Associative Arrays.
I know how to vectorize a function in C with macros to give results similar to the Mathematica's Map (or Apply) functionality. Namely apply a function to a list of arguments.
#define Apply( type, function, ...) \
{ \
void *Stop = (int[]){0}; \
type **List = (type*[]){__VA_ARGS__, Stop}; \
for( int i = 0; List[i] != Stop; ++i ) \
function( List[i] ); \
}
I can then do something like
#define FreeAllAtOnce(...) Apply( void, free, __VA_ARGS__ );
which has the effect that
free( Array1 );
free( Array2 );
free( Array3 );
is equivalent to
FreeAllAtOnce( Array1, Array2, Array3 );
I didn't make that up, I read about it in a book and have used it heavily since.
My question is: Can I do something similar to associatively combine an array via some binary function. For example take the GCD function. I want a function like:
GCD_all( a, b, c, d, e );
That has the same effect as
GCD( GCD( GCD( GCD( a, b ), c ), d ), e );
for any number of arguments.
I've tried to do this and have not been able to properly make it work. I'm also interested in the case where there may be additional parameters passed to the function. In the most generic sense I'm looking to do this with functions like:
Atype BinaryCombine( Atype a, Atype b, OtherType z, OtherType y )
so that I have a function
Atype BinaryCombineAll( Atype a, Atype b, Atype c, Atype d, OtherType z, OtherType y )
I hope that makes sense. Any ideas or help would be very appreciated!
Thanks.