Partially instantiating templates?

Andrej Mitrovic andrej.mitrovich at gmail.com
Mon Jan 31 08:52:24 PST 2011


On 1/31/11, Simen kjaeraas <simen.kjaras at gmail.com> wrote:
>
> module foo;
>
> import std.typecons;
> import std.functional;
> import std.array;
>
> template optArg( alias pred ) {
>      template optArg( alias fn ) {
>          auto optArg( Range )( Range r ) {
>              alias binaryFun!pred predicate;
>              alias unaryFun!fn func;
>
>              auto result = tuple( r.front, func( r.front ) );
>              foreach ( e; r ) {
>                  auto tmp = func( e );
>                  if ( predicate( e, result[1] ) ) {
>                      result = tuple( e, tmp );
>                  }
>              }
>              return result;
>          }
>      }
> }
>
> void main( ) {
>      alias optArg!"a<b" minArg;
>      alias minArg!"a" foo;
>      assert( foo( [5,2,1,3] ) == tuple(1,1) );
> }
>

Damn! That's pretty nice, I didn't know we could nest with the
eponymous trick. This could be quite useful, thanks.


More information about the Digitalmars-d-learn mailing list