Patterns to avoid GC with capturing closures?

aliak something at something.com
Sun Aug 26 11:38:08 UTC 2018


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 quite hacky to work like 
>> this.
>
> Here's a spoonful of sugar to help that go down easier:
>
> https://run.dlang.io/is/8lTmZg

And with some mixin magic you can get syntax like:

int a = 2, b = 3;
xs.pack(a, b).map!(unpack!((x, a, b) => x * a * b));

https://run.dlang.io/is/gb5Io4

Of course it sucks that you can't just tell a delegate to capture 
by value.


More information about the Digitalmars-d-learn mailing list