Replacing nested loops foreach using map/each/etc

Dennis Ritchie via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 25 07:25:51 PDT 2015


Hi,
Is it possible to write such a construction that could push 
immediately to a conditional statement without using nested 
loops? Ie organize search directly in the iterator if provided by 
a map / each / iota and other support functions.
Ie I want to write this code shorter :)

import std.stdio;

void main() {

     const x = 12, y = 65, z = 50, s = 1435;
	
     foreach (i; 0 .. x + 1)
         foreach (j; 0 .. x + 1)
             foreach (k; 0 .. x + 1)
                 if (i * (y + 3 * z) + j * (y + 2 * z) + k * (y + 
z) == s) {
                     writeln(i + j + k);
                     writeln(i * 3 + j * 2 + k);
                     writeln(i * 3 + j * 2 + k);
                     writeln(i, j, k);
                 }
}
-----
http://rextester.com/SJTU87854


More information about the Digitalmars-d-learn mailing list