foreach multiple loop sugar
Though sugar seems to be somewhat looked down upon I thought I'd suggest this- having seen the cartesianProduct function from std.algorithm in another thread I thought it would be an excellent piece of sugar in the language. It's not an earth shattering change but it makes something very common more elegant and reduces indentation significantly for multiple nested loops. Braces make nested loops very messy and any significant quantity of code in the loop body benefits from not being in a messy nesting. import std.algorithm, std.range, std.stdio; void main() { // Standard foreach(i; 0..10) foreach(j; 0..10) foreach(k; 0..10) writeln(i, j, k); // Better foreach(k, j, i; cartesianProduct(10.iota, 10.iota, 10.iota)) writeln(i, j, k); // Sugar foreach(k, j, i; 0..10, 0..10, 0..10) writeln(i, j, k); //Following brace rules // Standard foreach(i; 0..10) { foreach(j; 0..10) { foreach(k; 0..10) { writeln(i, j, k); } } } // Sugar foreach(k, j, i; 0..10, 0..10, 0..10) { writeln(i, j, k); } }
Re: foreach multiple loop sugar
On Tuesday, 18 August 2015 at 15:51:55 UTC, ixid wrote: Though sugar seems to be somewhat looked down upon I thought I'd suggest this- having seen the cartesianProduct function from std.algorithm in another thread I thought it would be an excellent piece of sugar in the language. It's not an earth shattering change but it makes something very common more elegant and reduces indentation significantly for multiple nested loops. Braces make nested loops very messy and any significant quantity of code in the loop body benefits from not being in a messy nesting. ... What would you do with associative arrays? void main() { auto aa = [1:1, 2:2]; foreach (a, b ; aa, 1..10) foo(a, b); }
Re: foreach multiple loop sugar
On Tuesday, 18 August 2015 at 15:51:55 UTC, ixid wrote: Though sugar seems to be somewhat looked down upon I thought I'd suggest this- having seen the cartesianProduct function from std.algorithm in another thread I thought it would be an excellent piece of sugar in the language. It's not an earth shattering change but it makes something very common more elegant and reduces indentation significantly for multiple nested loops. Braces make nested loops very messy and any significant quantity of code in the loop body benefits from not being in a messy nesting. import std.algorithm, std.range, std.stdio; void main() { // Standard foreach(i; 0..10) foreach(j; 0..10) foreach(k; 0..10) writeln(i, j, k); // Better foreach(k, j, i; cartesianProduct(10.iota, 10.iota, 10.iota)) writeln(i, j, k); // Sugar foreach(k, j, i; 0..10, 0..10, 0..10) writeln(i, j, k); //Following brace rules // Standard foreach(i; 0..10) { foreach(j; 0..10) { foreach(k; 0..10) { writeln(i, j, k); } } } // Sugar foreach(k, j, i; 0..10, 0..10, 0..10) { writeln(i, j, k); } You can create a multi-loop quite easily in template form for avoid the nesting. Essentially use an array for the the index instead of individual variables, e.g., mutliloop([0..10, 0..10, 0..10], (i)=> { writeln(i[0], i[1], i[2]); }); I'm not sure how efficient it is but essentially achieves what you are asking without too much overhead. Obviously having good language support is always nice...
Re: foreach multiple loop sugar
On Tuesday, 18 August 2015 at 16:02:42 UTC, cym13 wrote: On Tuesday, 18 August 2015 at 15:51:55 UTC, ixid wrote: Though sugar seems to be somewhat looked down upon I thought I'd suggest this- having seen the cartesianProduct function from std.algorithm in another thread I thought it would be an excellent piece of sugar in the language. It's not an earth shattering change but it makes something very common more elegant and reduces indentation significantly for multiple nested loops. Braces make nested loops very messy and any significant quantity of code in the loop body benefits from not being in a messy nesting. ... What would you do with associative arrays? void main() { auto aa = [1:1, 2:2]; foreach (a, b ; aa, 1..10) foo(a, b); } Prevent both iterator count and associative value variables for foreach loops with nested loops. This behaviour of associative arrays is already an odd case as it clashes with the iterator behaviour for other arrays.
Re: foreach multiple loop sugar
On Tuesday, 18 August 2015 at 15:51:55 UTC, ixid wrote: Though sugar seems to be somewhat looked down upon I thought I'd suggest this- having seen the cartesianProduct function from std.algorithm in another thread I thought it would be an excellent piece of sugar in the language. It's not an earth shattering change but it makes something very common more elegant and reduces indentation significantly for multiple nested loops. Braces make nested loops very messy and any significant quantity of code in the loop body benefits from not being in a messy nesting. ... What's wrong with just putting all the foreach statements on a single line? foreach(i; 0..10) foreach(j; 0..10) foreach(k; 0..10) { writeln(i, j, k); }
Re: foreach multiple loop sugar
On Tuesday, 18 August 2015 at 16:51:01 UTC, ixid wrote: On Tuesday, 18 August 2015 at 16:02:42 UTC, cym13 wrote: On Tuesday, 18 August 2015 at 15:51:55 UTC, ixid wrote: Though sugar seems to be somewhat looked down upon I thought I'd suggest this- having seen the cartesianProduct function from std.algorithm in another thread I thought it would be an excellent piece of sugar in the language. It's not an earth shattering change but it makes something very common more elegant and reduces indentation significantly for multiple nested loops. Braces make nested loops very messy and any significant quantity of code in the loop body benefits from not being in a messy nesting. ... What would you do with associative arrays? void main() { auto aa = [1:1, 2:2]; foreach (a, b ; aa, 1..10) foo(a, b); } Prevent both iterator count and associative value variables for foreach loops with nested loops. This behaviour of associative arrays is already an odd case as it clashes with the iterator behaviour for other arrays. It is not iterator count, it is key and value. And actually, it is pretty consistent. import std.stdio; void main () { foreach (index, value; [2, 4, 8]) writefln ("a[%s] = %s", index, value); foreach (index, value; ['x': 2, 'y': 4, 'z': 8]) writefln ("b[%s] = %s", index, value); } The output is: a[0] = 2 a[1] = 4 a[2] = 8 b[z] = 8 b[x] = 2 b[y] = 4
Re: foreach multiple loop sugar
On Tuesday, 18 August 2015 at 17:44:00 UTC, Xinok wrote: On Tuesday, 18 August 2015 at 15:51:55 UTC, ixid wrote: Though sugar seems to be somewhat looked down upon I thought I'd suggest this- having seen the cartesianProduct function from std.algorithm in another thread I thought it would be an excellent piece of sugar in the language. It's not an earth shattering change but it makes something very common more elegant and reduces indentation significantly for multiple nested loops. Braces make nested loops very messy and any significant quantity of code in the loop body benefits from not being in a messy nesting. ... What's wrong with just putting all the foreach statements on a single line? foreach(i; 0..10) foreach(j; 0..10) foreach(k; 0..10) { writeln(i, j, k); } This. And it's more obvious what you're trying to do.