I dun a DIP, possibly the best DIP ever
Steven Schveighoffer
schveiguy at gmail.com
Sun May 10 16:34:38 UTC 2020
On 5/10/20 4:30 AM, 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;
> }
> }
I admit, I didn't look at the implementation, I just assumed it was one
of the recursive ones.
But the "performance" is relative to where the satisfying items are. But
it's a good point, evaluating all of the templates to see if one is true
leaves some performance on the table, and another reason to prefer a
folding mechanism. i.e., this might short circuit automatically (if
supported):
enum anySatisfy(alias F, T...) = F!(T) || ...;
>
> Your versions also require an extra import (although canFind could be
> locally copied).
Yeah, a for loop CTFE can easily be created for this if needed. It was
just easier to reach for something that exists to show the brevity.
-Steve
More information about the Digitalmars-d
mailing list