Parallel foreach over AliasSec?

Bastiaan Veelo via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Feb 27 01:52:01 PST 2017


On Monday, 27 February 2017 at 02:02:57 UTC, ag0aep6g wrote:
> Make a range or an array of function pointers from the AliasSeq 
> of function aliases:
>
> ----
> import std.meta: staticMap;
> import std.range: only;
>
> enum fptr(alias f) = &f;
> enum fptrs = staticMap!(fptr, funcs);
> auto r = only(fptrs);
>
> foreach (i, f; parallel(r))
>     values[i] = f(val);
> ----

Although this answers my question perfectly, it turns out that I 
have simplified my case too much. It looks like existing 
overloads are complicating the matter. (I am actually trying to 
parallelise 
https://github.com/PhilippeSigaud/Pegged/blob/master/pegged/peg.d#L1646.)

The problem seems to be
> enum fptr(alias f) = &f;
(This is still a bit magical to me: it this a shorthand for a 
template?)

Can the following be made to work?

int one(int) {return 1;}
int two(int) {return 2;}
int three(int) {return 3;}
int four(int) {return 4;}
int five(int) {return 5;}
int six(int) {return 6;}
int seven(int) {return 7;}
int eight(int) {return 8;}

int one(string) {return 0;} // How to ignore this?

int[8] values;

template eval_all(funcs...)
{
     void eval_all(int val)
     {
         import std.meta: staticMap, Filter;
         import std.range: only;
         import std.parallelism;
         import std.traits;

         alias int function(int) iwant;
         //enum fptr(alias f) = &f;          // Error: cannot 
infer type from
                                             // overloaded 
function symbol & one
         enum fptr(alias int f(int)) = &f;   // ditto.
         enum fptrs = staticMap!(fptr, funcs);
         auto r = only(fptrs);

         foreach (i, f; parallel(r))
             values[i] = f(val);
     }
}

void main()
{
     eval_all!(one, two, three, four, five, six, seven, eight)(42);
     foreach(i, val; values)
         assert(val == i + 1);
}


More information about the Digitalmars-d-learn mailing list