Calling an Associative* Function with C Macros
<p>*Not to be confused with having anything to do with Associative Arrays.</p>

<p>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. </p>

<pre><code>#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] );                         \
}                     
</code></pre>

<p>I can then do something like</p>

<pre><code>#define FreeAllAtOnce(...) Apply( void, free, __VA_ARGS__ );
</code></pre>

<p>which has the effect that</p>

<pre><code>free( Array1 );
free( Array2 );
free( Array3 );
</code></pre>

<p>is equivalent to</p>

<pre><code>FreeAllAtOnce( Array1, Array2, Array3 );
</code></pre>

<p>I didn't make that up, I read about it in a book and have used it heavily since. </p>

<p><strong>My question is:</strong> 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:</p>

<pre><code>GCD_all( a, b, c, d, e );
</code></pre>

<p>That has the same effect as</p>

<pre><code>GCD( GCD( GCD( GCD( a, b ), c ), d ), e );
</code></pre>

<p>for any number of arguments.</p>

<p>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:</p>

<pre><code>Atype BinaryCombine( Atype a, Atype b, OtherType z, OtherType y )
</code></pre>

<p>so that I have a function</p>

<pre><code>Atype BinaryCombineAll( Atype a, Atype b, Atype c, Atype d, OtherType z, OtherType y )
</code></pre>

<p>I hope that makes sense. Any ideas or help would be very appreciated!</p>

<p>Thanks.</p>