I dun a DIP, possibly the best DIP ever

Stefan Koch uplink.coder at googlemail.com
Sun May 10 08:50:08 UTC 2020


On Sunday, 10 May 2020 at 08:30:35 UTC, Nick Treleaven wrote:
> On Friday, 24 April 2020 at 21:00:08 UTC, Steven Schveighoffer 
> wrote:
>> import std.algorithm : canFind;
>> enum anySatisfy(alias F, T...) = [F!(T)...].canFind(true);
>> enum allSatisfy(alias F, T...) = ![F!(T)...].canFind(false);
>
> That might be slower than the existing templates (now in 
> core.internal.traits), which don't use template recursion, and 
> short circuit:
>
> template anySatisfy(alias F, Ts...)
> {
>     static foreach (T; Ts)
>     {
>         static if (!is(typeof(anySatisfy) == bool) && // not 
> yet defined
>                    F!T)
>         {
>             enum anySatisfy = true;
>         }
>     }
>     static if (!is(typeof(anySatisfy) == bool)) // if not yet 
> defined
>     {
>         enum anySatisfy = false;
>     }
> }
>
> Your versions also require an extra import (although canFind 
> could be locally copied).

Here is how this would look as a type function.

bool anySatisfy(bool function (alias) pred, alias[]... types)
{
     foreach(type; types)
     {
         if (pred(type))
             return true;
     }
     return false;
}



More information about the Digitalmars-d mailing list