* Not to be confused with anything related to associative arrays.
I know how to vectorize a function in C using macros to give results similar to the functionality of Mathematica Map (or Apply). Namely, apply the function to the argument list.
#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] ); \ }
Then I can do something like
#define FreeAllAtOnce(...) Apply( void, free, __VA_ARGS__ );
which leads to the fact that
free( Array1 ); free( Array2 ); free( Array3 );
equivalently
FreeAllAtOnce( Array1, Array2, Array3 );
I did not do this, I read about it in the book and have used it ever since.
My question is: Can I do something similar to associative array joining through some binary function. For example, take the GCD function. I need a function like:
GCD_all( a, b, c, d, e );
This has the same effect as
GCD( GCD( GCD( GCD( a, b ), c ), d ), e );
for any number of arguments.
I tried to do this and could not properly get it to work. I am also interested in the case when additional functions are passed to a function. In the most general sense, I want to do this with functions such as:
Atype BinaryCombine( Atype a, Atype b, OtherType z, OtherType y )
so i have a function
Atype BinaryCombineAll( Atype a, Atype b, Atype c, Atype d, OtherType z, OtherType y )
Hope this makes sense. Any ideas or help would be greatly appreciated!
Thanks.