Replacing nested loops foreach using map/each/etc

Dennis Ritchie via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 25 10:52:08 PDT 2015


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; }
> (i) { return i; }
> { return 0; }
>
> http://dlang.org/expression.html#FunctionLiteral
> function

Thanks.

But why is the solution breaks down when `s = 10000` ? :)

import std.stdio, std.algorithm, std.range;

int c;
const x = 12, y = 65, z = 50, s = 100000;

void solve(Range)(Range r) {
	cartesianProduct(r, r, r).filter!(i => i[0] * (y + 3 * z) + i[1] 
* (y + 2 * z) + i[2] * (y + z) == s).each!dout;
}

void main() {

     auto a = iota(0, x + 1).array;

     solve(a);

     writefln(`%s total`, c);
}

void dout(Tuple)(Tuple idx) {
     ++c;
}
-----
http://rextester.com/XGDL26042


More information about the Digitalmars-d-learn mailing list