On 1/31/11, Simen kjaeraas <[email protected]> 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.

Reply via email to