Idiomatic way of writing nested loops?

Anton Fediushin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 17 04:07:35 PDT 2017


Hello! What is the best way of rewriting this code in idiomatic D 
manner?
------
foreach(a; ["foo", "bar"]) {
   foreach(b; ["baz", "foz", "bof"]) {
     foreach(c; ["FOO", "BAR"]) {
       // Some operations on a, b and c
     }
   }
}
------

Every array has at least 1 element, and adding/removing new 
"nested loops" should be as easy as possible.

Also, I have a question about running this in parallel: if I want 
to use nested loops with `parallel` from `std.parallelism`, 
should I add `parallel` to every loop like this?
------
foreach(a; ["foo", "bar"].parallel) {
   foreach(b; ["baz", "foz", "bof"].parallel) {
     foreach(c; ["FOO", "BAR"].parallel) {
       // Some operations on a, b and c
     }
   }
}
------
I am worried about running thousands of threads, because in this 
case first `parallel` runs 2 tasks, every task runs 3 tasks and 
every task runned inside a task runs 2 more tasks.

So, how to write this in idiomatic D manner and run it _if 
possible_ in parallel?


More information about the Digitalmars-d-learn mailing list