parallel foreach

Nicholas Wilson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 1 06:24:55 PDT 2017


On Monday, 1 May 2017 at 12:42:01 UTC, Alex wrote:
> Hi all,
> the last foreach in the following code does not compile... Is 
> this a bug, or is something wrong with my syntax?
>
> void main()
> {
> 	import std.parallelism : parallel;
> 	import std.range : iota;
> 	
> 	foreach(i; iota(0, 5)){}
> 	foreach(i; staticIota!(0, 5)){}
> 	foreach(i; parallel(iota(0, 5))){}
> 	foreach(i; parallel(staticIota!(0, 5))){}
> }
>
> // copied from core.internal.traits
> template staticIota(int beg, int end)
> {
> 	import std.typetuple;
>     static if (beg + 1 >= end)
>     {
>         static if (beg >= end)
>         {
>             alias staticIota = TypeTuple!();
>         }
>         else
>         {
>             alias staticIota = TypeTuple!(+beg);
>         }
>     }
>     else
>     {
>         enum mid = beg + (end - beg) / 2;
>         alias staticIota = TypeTuple!(staticIota!(beg, mid), 
> staticIota!(mid, end));
>     }
> }

Because staticIota expands to 0,1,2,3,4
so you are trying todo
foreach(i; parallel(0,1,2,3,4){}
which doesn't work.
try
foreach(i; parallel([staticIota!(0, 5)])){}
or
foreach(i; parallel(only(staticIota!(0, 5)))){}
not sure of the second one will work.


More information about the Digitalmars-d-learn mailing list