Re: How to provide this arg or functor for algorithm?

2015-08-17 Thread anonymous via Digitalmars-d-learn
On Monday, 17 August 2015 at 16:21:16 UTC, thedeemon wrote: On Monday, 17 August 2015 at 16:18:50 UTC, thedeemon wrote: I've just checked with my runtime GC hook. Here the call to func() allocates 12 bytes via gc_malloc, and it's the same for a 4-elements array, so it's not for the array itself

Re: How to provide this arg or functor for algorithm?

2015-08-17 Thread thedeemon via Digitalmars-d-learn
On Monday, 17 August 2015 at 16:18:50 UTC, thedeemon wrote: I've just checked with my runtime GC hook. Here the call to func() allocates 12 bytes via gc_malloc, and it's the same for a 4-elements array, so it's not for the array itself, it's for a closure, I think. Also, compiling with -vgc s

Re: How to provide this arg or functor for algorithm?

2015-08-17 Thread thedeemon via Digitalmars-d-learn
On Monday, 17 August 2015 at 12:38:05 UTC, anonymous wrote: auto func()(uint[] arr, uint delegate(uint) pure @nogc d) @nogc { return arr.map!(d); } void main() @nogc { uint[3] arr = [1,2,3]; uint context = 2; auto c = Caller(context); auto d = &c.method; auto r = func(ar

Re: How to provide this arg or functor for algorithm?

2015-08-17 Thread anonymous via Digitalmars-d-learn
On Monday, 17 August 2015 at 10:28:33 UTC, thedeemon wrote: Nope, it "works" only because "r" is unreferenced and gets thrown out. Just try using r.front there, for example, and the error returns. You're right, it falls short. But I think r not being referenced is not exactly it. Using front

Re: How to provide this arg or functor for algorithm?

2015-08-17 Thread thedeemon via Digitalmars-d-learn
On Monday, 17 August 2015 at 09:51:47 UTC, anonymous wrote: Huh. I think func being a template is the key here. When the original code is put in a template, it works too (with 2.068): Nope, it "works" only because "r" is unreferenced and gets thrown out. Just try using r.front there, for examp

Re: How to provide this arg or functor for algorithm?

2015-08-17 Thread anonymous via Digitalmars-d-learn
On Sunday, 16 August 2015 at 23:05:42 UTC, Ali Çehreli wrote: // Now the type of d is a template parameter @nogc auto func(Func)(uint[] arr, Func d) { return arr.map!(d); } Huh. I think func being a template is the key here. When the original code is put in a template, it works too (with 2

Re: How to provide this arg or functor for algorithm?

2015-08-16 Thread Ali Çehreli via Digitalmars-d-learn
On 08/16/2015 03:36 PM, cym13 wrote: On Sunday, 16 August 2015 at 22:22:07 UTC, Ali Çehreli wrote: // HERE: // Error: function deneme.func @nogc function allocates //a closure with the GC @nogc auto func(uint[] arr, DelegateRef d) { return arr.map!(a => d.d(a)); } Aren't you makin

Re: How to provide this arg or functor for algorithm?

2015-08-16 Thread cym13 via Digitalmars-d-learn
On Sunday, 16 August 2015 at 22:22:07 UTC, Ali Çehreli wrote: // HERE: // Error: function deneme.func @nogc function allocates //a closure with the GC @nogc auto func(uint[] arr, DelegateRef d) { return arr.map!(a => d.d(a)); } Aren't you making another delegate in the map by using

Re: How to provide this arg or functor for algorithm?

2015-08-16 Thread Ali Çehreli via Digitalmars-d-learn
On 08/16/2015 09:26 AM, FreeSlave wrote: >> It still says it needs allocation: >> >> test.d(17): Error: function test.func @nogc function allocates a >> closure with the GC I wrapped it inside a class object but it still thinks it needs to allocate: import std.stdio; import std.range; import st

Re: How to provide this arg or functor for algorithm?

2015-08-16 Thread FreeSlave via Digitalmars-d-learn
On Sunday, 16 August 2015 at 16:23:05 UTC, FreeSlave wrote: On Sunday, 16 August 2015 at 15:29:10 UTC, Ali Çehreli wrote: On 08/16/2015 04:53 AM, FreeSlave wrote: > The problem is that this allocates delegate, so it can't be used in > @nogc code. Would constructing the delegate by setting its

Re: How to provide this arg or functor for algorithm?

2015-08-16 Thread FreeSlave via Digitalmars-d-learn
On Sunday, 16 August 2015 at 15:29:10 UTC, Ali Çehreli wrote: On 08/16/2015 04:53 AM, FreeSlave wrote: > The problem is that this allocates delegate, so it can't be used in > @nogc code. Would constructing the delegate by setting its .funcptr and .ptr properties work in this case? You can have

Re: How to provide this arg or functor for algorithm?

2015-08-16 Thread Ali Çehreli via Digitalmars-d-learn
On 08/16/2015 04:53 AM, FreeSlave wrote: > The problem is that this allocates delegate, so it can't be used in > @nogc code. Would constructing the delegate by setting its .funcptr and .ptr properties work in this case? You can have a pool of context objects which become the context for the de

Re: How to provide this arg or functor for algorithm?

2015-08-16 Thread FreeSlave via Digitalmars-d-learn
On Sunday, 16 August 2015 at 12:30:54 UTC, cym13 wrote: On Sunday, 16 August 2015 at 11:53:42 UTC, FreeSlave wrote: [...] Ok, so as my lambda proposition obviously doesn't work, here is one way that does using a templated function. There may be a way to make it shorter, I don't know.

Re: How to provide this arg or functor for algorithm?

2015-08-16 Thread cym13 via Digitalmars-d-learn
On Sunday, 16 August 2015 at 11:53:42 UTC, FreeSlave wrote: Let's say I want to map some range using some context. The obvious way is to do: uint[3] arr = [1,2,3]; uint context = 2; auto r = arr[].map!(delegate(value) { return value * context; }); The problem is that this allocates delegate,

Re: How to provide this arg or functor for algorithm?

2015-08-16 Thread Adam D. Ruppe via Digitalmars-d-learn
On Sunday, 16 August 2015 at 12:04:51 UTC, cym13 wrote: To me the obvious way is to use a lambda, not a delegate: Lambdas and delegates are the same thing, just different syntax.

Re: How to provide this arg or functor for algorithm?

2015-08-16 Thread cym13 via Digitalmars-d-learn
On Sunday, 16 August 2015 at 11:53:42 UTC, FreeSlave wrote: Let's say I want to map some range using some context. The obvious way is to do: uint[3] arr = [1,2,3]; uint context = 2; auto r = arr[].map!(delegate(value) { return value * context; }); To me the obvious way is to use a lambda, not

How to provide this arg or functor for algorithm?

2015-08-16 Thread FreeSlave via Digitalmars-d-learn
Let's say I want to map some range using some context. The obvious way is to do: uint[3] arr = [1,2,3]; uint context = 2; auto r = arr[].map!(delegate(value) { return value * context; }); The problem is that this allocates delegate, so it can't be used in @nogc code. What I want to do might lo