Jump to page: 1 2
Thread overview
They are not the same
Apr 05, 2014
bearophile
Apr 05, 2014
Meta
Apr 05, 2014
Meta
Apr 05, 2014
John Colvin
Apr 05, 2014
bearophile
Apr 05, 2014
Timon Gehr
Apr 05, 2014
bearophile
Apr 05, 2014
Artur Skawina
Apr 05, 2014
Timon Gehr
Apr 09, 2014
Kagamin
Apr 09, 2014
Timon Gehr
Apr 10, 2014
Kagamin
April 05, 2014
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
April 05, 2014
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]?
April 05, 2014
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.
April 05, 2014
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.
April 05, 2014
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
April 05, 2014
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
April 05, 2014
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
April 05, 2014
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
April 05, 2014
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
>

April 09, 2014
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?
« First   ‹ Prev
1 2