Thread overview
parallel foreach
May 01, 2017
Alex
May 01, 2017
Nicholas Wilson
May 01, 2017
Alex
May 01, 2017
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));
    }
}
May 01, 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.
May 01, 2017
On Monday, 1 May 2017 at 13:24:55 UTC, Nicholas Wilson wrote:
> 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.

Ah thanks. Both versions work.