Operators as function arguments?

Daniel Keep daniel.keep.lists at gmail.com
Sat Mar 24 19:05:24 PDT 2007



Jarrett Billingsley wrote:
> "Falk Henrich" <schreibmalwieder at hammerfort.de> wrote in message 
> news:eu1cm8$k2i$1 at digitalmars.com...
>> But this is extremely clumsy. Any idea?
>>
> 
> Perhaps?
> 
> typeof(a[0]) reduce(alias a, alias func)()
> {
>     auto ret = a[0];
> 
>     foreach(v; a[1 .. $])
>         ret = func(ret, v);
> 
>     return ret;
> }
> 
> T[] cat(T)(T[] a, T[] b)
> {
>     return a ~ b;
> }
> 
> void main()
> {
>     int[][] a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
>     int[] b;
> 
>     b = reduce!(a, cat)();
>     writefln(b);
> }
> 
> Using the alias parameter for the function means you can no longer use 
> delegate literals for the function, but.. 

Interesting.  I'd never thought of doing it that way. :)

I do actually use the delegate literals in a few places, tho, so it
would likely annoy me... plus, having !(...)() looks hackish. :P

	-- Daniel

-- 
int getRandomNumber()
{
    return 4; // chosen by fair dice roll.
              // guaranteed to be random.
}

http://xkcd.com/

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/


More information about the Digitalmars-d-learn mailing list