Re: Patterns to avoid GC with capturing closures?

2018-08-26 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 26 August 2018 at 06:08:39 UTC, vit wrote: const x = iota(0, 10) .map!((x, i) => x*i)(a) ///map!((x) => x*a) .map!((x, i) => x*i)(b) ///map!((x) => x*b) .filter!((x, i) => x%i)(c)///filter!((x) => x%c) .any!(x => x % c); I think it's easier to

Re: Patterns to avoid GC with capturing closures?

2018-08-26 Thread aliak via Digitalmars-d-learn
On Friday, 24 August 2018 at 22:51:40 UTC, Paul Backus wrote: On Friday, 24 August 2018 at 15:18:13 UTC, Peter Alexander wrote: I can write scaleAll like this: auto scaleAll(int[] xs, int m) @nogc { return repeat(m).zip(xs).map!(mx => mx[0] * mx[1]); } So that repeat(m) stores m, but it is

Re: Patterns to avoid GC with capturing closures?

2018-08-26 Thread vit via Digitalmars-d-learn
On Friday, 24 August 2018 at 15:18:13 UTC, Peter Alexander wrote: Consider this code, which is used as an example only: auto scaleAll(int[] xs, int m) { return xs.map!(x => m * x); } As m is captured, the delegate for map will rightly allocate the closure in the GC heap. In C++, you would

Re: Patterns to avoid GC with capturing closures?

2018-08-24 Thread Paul Backus via Digitalmars-d-learn
On Friday, 24 August 2018 at 15:18:13 UTC, Peter Alexander wrote: I can write scaleAll like this: auto scaleAll(int[] xs, int m) @nogc { return repeat(m).zip(xs).map!(mx => mx[0] * mx[1]); } So that repeat(m) stores m, but it is quite hacky to work like this. Here's a spoonful of sugar to

Re: Patterns to avoid GC with capturing closures?

2018-08-24 Thread Steven Schveighoffer via Digitalmars-d-learn
On 8/24/18 11:18 AM, Peter Alexander wrote: Consider this code, which is used as an example only: auto scaleAll(int[] xs, int m) {   return xs.map!(x => m * x); } As m is captured, the delegate for map will rightly allocate the closure in the GC heap. In C++, you would write the lambda to

Patterns to avoid GC with capturing closures?

2018-08-24 Thread Peter Alexander via Digitalmars-d-learn
Consider this code, which is used as an example only: auto scaleAll(int[] xs, int m) { return xs.map!(x => m * x); } As m is captured, the delegate for map will rightly allocate the closure in the GC heap. In C++, you would write the lambda to capture m by value, but this is not a