www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - They are not the same

reply "bearophile" <bearophileHUGS lycos.com> writes:
Can you spot the difference between foo1 and foo2?


import std.algorithm: map;
import std.range: iota;

void foo1(in int[] a, in int[] b) pure {
     int[] r;
     foreach (immutable i; 0 .. a.length)
         r ~= (i % 2) ? a[i] : b[i];
}

void foo2(in int[] a, in int[] b) pure {
     int[] r;
     foreach (x; iota(a.length)
                 .map!(i => (i % 2) ? a[i] : b[i]))
         r ~= x;
}

void main() {}



Sometimes variants of this problem hit me. I don't even know if 
this simple problem has a name. Is it impossible to solve?

Bye,
bearophile
Apr 04 2014
next sibling parent reply "Meta" <jared771 gmail.com> writes:
On Saturday, 5 April 2014 at 01:28:06 UTC, bearophile wrote:
 Can you spot the difference between foo1 and foo2?


 import std.algorithm: map;
 import std.range: iota;

 void foo1(in int[] a, in int[] b) pure {
     int[] r;
     foreach (immutable i; 0 .. a.length)
         r ~= (i % 2) ? a[i] : b[i];
 }

 void foo2(in int[] a, in int[] b) pure {
     int[] r;
     foreach (x; iota(a.length)
                 .map!(i => (i % 2) ? a[i] : b[i]))
         r ~= x;
 }

 void main() {}



 Sometimes variants of this problem hit me. I don't even know if 
 this simple problem has a name. Is it impossible to solve?

 Bye,
 bearophile
When I put a println inside both functions, they both print out [2, 1, 0]. Shouldn't the first print [0, 1, 2] while the second prints [2, 1, 0]?
Apr 04 2014
parent "Meta" <jared771 gmail.com> writes:
On Saturday, 5 April 2014 at 01:57:59 UTC, Meta wrote:
 When I put a println inside both functions, they both print out 
 [2, 1, 0]. Shouldn't the first print [0, 1, 2] while the second 
 prints [2, 1, 0]?
No, sorry, I was mistaken about what was going on.
Apr 04 2014
prev sibling parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Saturday, 5 April 2014 at 01:28:06 UTC, bearophile wrote:
 Can you spot the difference between foo1 and foo2?


 import std.algorithm: map;
 import std.range: iota;

 void foo1(in int[] a, in int[] b) pure {
     int[] r;
     foreach (immutable i; 0 .. a.length)
         r ~= (i % 2) ? a[i] : b[i];
 }

 void foo2(in int[] a, in int[] b) pure {
     int[] r;
     foreach (x; iota(a.length)
                 .map!(i => (i % 2) ? a[i] : b[i]))
         r ~= x;
 }

 void main() {}



 Sometimes variants of this problem hit me. I don't even know if 
 this simple problem has a name. Is it impossible to solve?

 Bye,
 bearophile
Pity... I think there's an argument that this should work, on the grounds that the context pointer is just another argument and therefore the lambda can be weakly pure.
Apr 05 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
John Colvin:

 I think there's an argument that this should work, on the 
 grounds that the context pointer is just another argument and 
 therefore the lambda can be weakly pure.
Was this discussed in the forum? Do you think you can ask for an enhancement in Bugzilla? Bye, bearophile
Apr 05 2014
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 04/05/2014 11:53 AM, bearophile wrote:
 John Colvin:

 I think there's an argument that this should work, on the grounds that
 the context pointer is just another argument and therefore the lambda
 can be weakly pure.
Was this discussed in the forum?
I've been bringing this up time and time again, but it is usually ignored.
 Do you think you can ask for an enhancement in Bugzilla?
 ...
It's a plain bug. In fact, you have commented on it: https://d.puremagic.com/issues/show_bug.cgi?id=9148
Apr 05 2014
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Timon Gehr:

 It's a plain bug. In fact, you have commented on it:

 https://d.puremagic.com/issues/show_bug.cgi?id=9148
That issue is significant. Thank you Timon. Apparently my memory is not very good :-) Bye, bearophile
Apr 05 2014
prev sibling parent reply Artur Skawina <art.08.09 gmail.com> writes:
On 04/05/14 21:51, Timon Gehr wrote:
 On 04/05/2014 11:53 AM, bearophile wrote:
 John Colvin:

 I think there's an argument that this should work, on the grounds that
 the context pointer is just another argument and therefore the lambda
 can be weakly pure.
Was this discussed in the forum?
I've been bringing this up time and time again, but it is usually ignored.
 Do you think you can ask for an enhancement in Bugzilla?
 ...
It's a plain bug. In fact, you have commented on it: https://d.puremagic.com/issues/show_bug.cgi?id=9148
You're asking for bypassing immutability there (The first 'bar'. That second 'bar' does not make sense w/o properly typed delegates; the second 'foo' should indeed work). artur
Apr 05 2014
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 04/05/2014 10:33 PM, Artur Skawina wrote:
 On 04/05/14 21:51, Timon Gehr wrote:
 On 04/05/2014 11:53 AM, bearophile wrote:
 John Colvin:

 I think there's an argument that this should work, on the grounds that
 the context pointer is just another argument and therefore the lambda
 can be weakly pure.
Was this discussed in the forum?
I've been bringing this up time and time again, but it is usually ignored.
 Do you think you can ask for an enhancement in Bugzilla?
 ...
It's a plain bug. In fact, you have commented on it: https://d.puremagic.com/issues/show_bug.cgi?id=9148
You're asking for bypassing immutability there (The first 'bar'.
Good point, presumably this was a copy-and-paste error.
 That second 'bar' does not make sense w/o properly typed delegates;
The issue asks for delegates to be typed properly, unless I misunderstand what 'properly' means here.
 the second 'foo' should indeed work).

 artur
Apr 05 2014
parent reply "Kagamin" <spam here.lot> writes:
On Saturday, 5 April 2014 at 21:33:30 UTC, Timon Gehr wrote:
 The issue asks for delegates to be typed properly, unless I 
 misunderstand what 'properly' means here.
Isn't the issue in pure nested functions treated as strongly pure? What does it have to do with delegates?
Apr 09 2014
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 04/09/2014 07:27 PM, Kagamin wrote:
 On Saturday, 5 April 2014 at 21:33:30 UTC, Timon Gehr wrote:
 The issue asks for delegates to be typed properly, unless I
 misunderstand what 'properly' means here.
Isn't the issue in pure nested functions treated as strongly pure? What does it have to do with delegates?
The issue is that pure nested functions and delegate literals are not allowed mutable access to data reachable using context pointers. However, this may also be exactly what is wanted in certain circumstances. 'Delegates are typed properly', I interpreted as meaning for one thing (besides fixing the unsoundness issues) that the 'immutable'/'const' qualifiers can be used on nested functions/delegate literals, which then restores the current behaviour in the case of immutable (i.e. only immutable state can be accessed through context pointers thus annotated.), and would prevent updates to data in the context in the case of const.
Apr 09 2014
parent "Kagamin" <spam here.lot> writes:
Pure functions are allowed to access mutable state without const 
qualifier. I don't understand, why you want the qualifier and how 
it would help with consistent implementation of purity.
Apr 10 2014