Partial template application

Simen kjaeraas simen.kjaras at gmail.com
Mon Jan 31 10:55:05 PST 2011


bearophile <bearophileHUGS at lycos.com> wrote:

> If (as usual) you don't want to implement this as a new D feature, I  
> presume something like a  
> PartialTemplate/StaticCurry/StaticPartialApplication is writeable in the  
> standard library:
>
> import std.typecons, std.functional, std.array;
>
> auto optArg(alias pred, alias fn, 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);
>     }
> }
>
> void main() {
>     alias PartialTemplate!(optArg, "a < b") minArg;
>     alias PartialTemplate!(minArg, "a") foo;
>     assert(foo([5, 2, 1, 3]) == tuple(1, 1));
> }

dranges has something like this:
http://svn.dsource.org/projects/dranges/trunk/dranges/docs/templates.html
see CurryTemplate

-- 
Simen


More information about the Digitalmars-d mailing list