Re: Replacing nested loops foreach using map/each/etc

2015-05-25 Thread Meta via Digitalmars-d-learn
On Monday, 25 May 2015 at 15:46:54 UTC, Dennis Ritchie wrote: On Monday, 25 May 2015 at 15:06:45 UTC, Alex Parrill wrote: Hint: Use `cartesianProduct` [1] with three iota ranges to replace the foreachs, and `filter` to replace the if [1]

Replacing nested loops foreach using map/each/etc

2015-05-25 Thread Dennis Ritchie via Digitalmars-d-learn
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

Re: Replacing nested loops foreach using map/each/etc

2015-05-25 Thread Dennis Ritchie via Digitalmars-d-learn
On Monday, 25 May 2015 at 15:06:45 UTC, Alex Parrill wrote: Hint: Use `cartesianProduct` [1] with three iota ranges to replace the foreachs, and `filter` to replace the if [1] http://dlang.org/phobos/std_algorithm_setops.html#.cartesianProduct Thank you. Is it possible to replace the loop

Re: Replacing nested loops foreach using map/each/etc

2015-05-25 Thread Alex Parrill via Digitalmars-d-learn
On Monday, 25 May 2015 at 14:25:52 UTC, Dennis Ritchie wrote: 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

Re: Replacing nested loops foreach using map/each/etc

2015-05-25 Thread Dennis Ritchie via Digitalmars-d-learn
On Monday, 25 May 2015 at 16:41:35 UTC, Meta wrote: import std.algorithm; import std.range; import std.stdio; void main() { const x = 12, y = 65, z = 50, s = 1435; auto a = iota(0, x + 1); cartesianProduct(a, a, a) .filter!(i = i[0] * (y + 3 * z) + i[1] * (y + 2

Re: Replacing nested loops foreach using map/each/etc

2015-05-25 Thread Meta via Digitalmars-d-learn
On Monday, 25 May 2015 at 17:05:35 UTC, Dennis Ritchie wrote: On Monday, 25 May 2015 at 16:41:35 UTC, Meta wrote: import std.algorithm; import std.range; import std.stdio; void main() { const x = 12, y = 65, z = 50, s = 1435; auto a = iota(0, x + 1); cartesianProduct(a, a, a)

Re: Replacing nested loops foreach using map/each/etc

2015-05-25 Thread Dennis Ritchie via Digitalmars-d-learn
On Monday, 25 May 2015 at 19:16:04 UTC, anonymous wrote: On Monday, 25 May 2015 at 17:52:09 UTC, Dennis Ritchie wrote: But why is the solution breaks down when `s = 1` ? :) import std.stdio, std.algorithm, std.range; int c; const x = 12, y = 65, z = 50, s = 10; Which is it, now? 4

Re: Replacing nested loops foreach using map/each/etc

2015-05-25 Thread anonymous via Digitalmars-d-learn
On Monday, 25 May 2015 at 17:52:09 UTC, Dennis Ritchie wrote: But why is the solution breaks down when `s = 1` ? :) import std.stdio, std.algorithm, std.range; int c; const x = 12, y = 65, z = 50, s = 10; Which is it, now? 4 or 5 zeros? void solve(Range)(Range r) {

Re: Replacing nested loops foreach using map/each/etc

2015-05-25 Thread Dennis Ritchie via Digitalmars-d-learn
On Monday, 25 May 2015 at 17:19:27 UTC, Meta wrote: `each` doesn't support braces. There are 4 ways to write a function/delegate literal in D (with a few minor variations): Short form: function(int i) = i; (int i) = i (i) = i i = i Long form: function(int i) { return i; } (int i) { return i;