Re: cannot access frame of function

2018-03-23 Thread Alex via Digitalmars-d-learn

On Friday, 23 March 2018 at 03:38:22 UTC, Jordan Wilson wrote:


I have this same issue. Anyone have any thoughts on a 
workaround?



For completeness:
https://issues.dlang.org/show_bug.cgi?id=17841



Re: cannot access frame of function

2018-03-22 Thread Jordan Wilson via Digitalmars-d-learn

On Monday, 18 September 2017 at 21:58:39 UTC, Alex wrote:

On Monday, 18 September 2017 at 18:49:54 UTC, ag0aep6g wrote:


Doesn't work for me. This still fails compilation with the 
same error:



import std.algorithm.iteration : sum, cumulativeFold;
void main()
{
double[5] a;
auto asum = 1.23;
auto jProbs = a[].cumulativeFold!((a, b) => (a + b)/asum);
}



So, this is a bug, isn't it? I assume, I should file it then...


I have this same issue. Anyone have any thoughts on a workaround?

For example, my attempt at an exponential moving average doesn't 
compile:

auto ema(Range,T) (Range rng, int period, T seed) {
return rng.cumulativeFold!((a,b) => 
a+(2.0/(period+1))*(b-a))(seed.to!double);

}

So I ended up with this:
auto ema(Range,T) (Range rng, int period, T seed) {
struct EMA(Range) {
double currentValue;
double weighting;
Range rng;
this (Range r, int p, double s) {
currentValue = s;
weighting = 2.0 / (p+1);
rng = r;
}
auto front() { return currentValue; }
auto popFront() {
rng.popFront;
if (!rng.empty){
currentValue += (rng.front - 
currentValue)*weighting;

}
}
auto empty() { return rng.empty; }
}
return EMA!Range(rng,period,seed.to!double);
}

Thanks,

Jordan




Re: Lambda cannot access frame of function

2017-11-18 Thread Ali Çehreli via Digitalmars-d-learn

On 11/18/2017 06:22 AM, kerdemdemir wrote:

//DMD64 D Compiler 2.072.2
import std.stdio;

bool foo(  bool function( double ) controlFoo )
{
     return controlFoo(5.0);
}

void foo2( double val )
{
     writeln  ( foo( a => a > val ) );
}

void main()
{
     foo2(20);
     writeln("Hello, World!");
}

Does not compile and gives this errors:
source_file.d(12): Error: function source.foo2.__lambda2 cannot access 
frame of function source.foo2


foo should take 'delegate'.

If you want it to work with any callable entity, take the function as an 
alias template parameter:


bool foo(alias controlFoo)()
{
return controlFoo(5.0);
}

void foo2( double val )
{
writeln  ( foo!( a => a > val )() );
}

Ali


Re: Lambda cannot access frame of function

2017-11-18 Thread Adam D. Ruppe via Digitalmars-d-learn

On Saturday, 18 November 2017 at 14:22:19 UTC, kerdemdemir wrote:

bool foo(  bool function( double ) controlFoo )


Change that `function` to `delegate` and it should work.

Function pointers aren't allowed to access other locals, but 
delegates are.


Re: Lambda cannot access frame of function

2017-11-18 Thread kerdemdemir via Digitalmars-d-learn
On Saturday, 18 November 2017 at 14:30:29 UTC, Adam D. Ruppe 
wrote:
On Saturday, 18 November 2017 at 14:22:19 UTC, kerdemdemir 
wrote:

bool foo(  bool function( double ) controlFoo )


Change that `function` to `delegate` and it should work.

Function pointers aren't allowed to access other locals, but 
delegates are.


Yes it worked as you suggested.

Thanks.


Lambda cannot access frame of function

2017-11-18 Thread kerdemdemir via Digitalmars-d-learn

//DMD64 D Compiler 2.072.2
import std.stdio;

bool foo(  bool function( double ) controlFoo )
{
return controlFoo(5.0);
}

void foo2( double val )
{
writeln  ( foo( a => a > val ) );
}

void main()
{
foo2(20);
writeln("Hello, World!");
}

Does not compile and gives this errors:
source_file.d(12): Error: function source.foo2.__lambda2 cannot 
access frame of function source.foo2
source_file.d(12): Error: function source.foo (bool 
function(double) controlFoo) is not callable using argument types 
(void)


Live example:
http://rextester.com/WRKCWR55408

Is there any workaround for that?

Regards
Erdem



Re: cannot access frame of function

2017-09-18 Thread Alex via Digitalmars-d-learn

On Monday, 18 September 2017 at 18:49:54 UTC, ag0aep6g wrote:


Doesn't work for me. This still fails compilation with the same 
error:



import std.algorithm.iteration : sum, cumulativeFold;
void main()
{
double[5] a;
auto asum = 1.23;
auto jProbs = a[].cumulativeFold!((a, b) => (a + b)/asum);
}



So, this is a bug, isn't it? I assume, I should file it then...




Re: cannot access frame of function

2017-09-18 Thread user1234 via Digitalmars-d-learn

On Monday, 18 September 2017 at 18:49:54 UTC, ag0aep6g wrote:

On 09/18/2017 08:25 PM, user1234 wrote:

On Monday, 18 September 2017 at 14:45:25 UTC, Alex wrote:

[...]

import std.algorithm.iteration : sum, cumulativeFold;

void main()
{
double[5] a;

[...]>> auto asum = a[].sum;
[...]

asum is a lazy range and the error comes from this.


asum is not a range. It's a double.

Let's say that if you replace  asum by 1.23 then the code 
compiles.


Doesn't work for me. This still fails compilation with the same 
error:



import std.algorithm.iteration : sum, cumulativeFold;
void main()
{
double[5] a;
auto asum = 1.23;
auto jProbs = a[].cumulativeFold!((a, b) => (a + b)/asum);
}



Aw right, i had put 1.23 directly in the lambda. So, OP, you can 
dismiss my comment.


Re: cannot access frame of function

2017-09-18 Thread ag0aep6g via Digitalmars-d-learn

On 09/18/2017 08:25 PM, user1234 wrote:

On Monday, 18 September 2017 at 14:45:25 UTC, Alex wrote:

[...]

import std.algorithm.iteration : sum, cumulativeFold;

void main()
{
double[5] a;

[...]>> auto asum = a[].sum;
[...]

asum is a lazy range and the error comes from this.


asum is not a range. It's a double.


Let's say that if you replace  asum by 1.23 then the code compiles.


Doesn't work for me. This still fails compilation with the same error:


import std.algorithm.iteration : sum, cumulativeFold;
void main()
{
double[5] a;
auto asum = 1.23;
auto jProbs = a[].cumulativeFold!((a, b) => (a + b)/asum);
}



Re: cannot access frame of function

2017-09-18 Thread user1234 via Digitalmars-d-learn

On Monday, 18 September 2017 at 14:45:25 UTC, Alex wrote:

Hi all,
given this code:

import std.algorithm.iteration : sum, cumulativeFold;

void main()
{
double[5] a;
a = 0;
foreach(el; a) assert(el == 0);
a[0] = 1.0;
a[1] = 2.0;
a[2] = 3.0;
a[3] = 4.0;
a[4] = 5.0;
foreach(el; a) assert(el != 0);
auto asum = a[].sum;
auto jProbs = a[].cumulativeFold!((a, b) => (a + b)/asum);
}

the last line does not compile.

I found
http://forum.dlang.org/post/mailman.4097.1499105927.31550.digitalmars-d-b...@puremagic.com
and
https://issues.dlang.org/show_bug.cgi?id=11886

What I do not understand, how my example differs from the fixed 
bug?


asum is a lazy range and the error comes from this.
Let's say that if you replace  asum by 1.23 then the code 
compiles.


cannot access frame of function

2017-09-18 Thread Alex via Digitalmars-d-learn

Hi all,
given this code:

import std.algorithm.iteration : sum, cumulativeFold;

void main()
{
double[5] a;
a = 0;
foreach(el; a) assert(el == 0);
a[0] = 1.0;
a[1] = 2.0;
a[2] = 3.0;
a[3] = 4.0;
a[4] = 5.0;
foreach(el; a) assert(el != 0);
auto asum = a[].sum;
auto jProbs = a[].cumulativeFold!((a, b) => (a + b)/asum);
}

the last line does not compile.

I found
http://forum.dlang.org/post/mailman.4097.1499105927.31550.digitalmars-d-b...@puremagic.com
and
https://issues.dlang.org/show_bug.cgi?id=11886

What I do not understand, how my example differs from the fixed 
bug?