On Mon, 05 Oct 2015 09:10:42 -0700, grond...@yahoo.fr wrote:
> The following two expressions should give the same result:
> 
> $ perl6 -e 'say ([o] (1 + 1/*), (2 + 1/*))(Inf);'
> 1.5
> 
> $ perl6 -e 'say ([o] (++$ + 1/*) xx 2)(Inf);'
> 3

What's happening is that the `++$` operation isn't evaluated only once (when 
the WhateverCode is constructed), but actually becomes part of the WhateverCode 
and is evaluated each time the WhateverCode is called:

    my &a = ++$ + *;
    say a 0;  # 1
    say a 0;  # 2
    say a 0;  # 3

That's nothing specific to `++` or `$`, e.g. consider:

    my &a = rand + *;
    say a 0;  # 0.949744965798848
    say a 0;  # 0.842127241122708
    say a 0;  # 0.716049843395635

As jnthn explained on IRC:

    ‎<‎jnthn‎>‎ Yeah, if the + is being whatever-curried then the
            LHS is going to be pulled into the thunk as a whole
    ‎<‎jnthn‎>‎ It's a compile-time transform

To create a different WhateverCode on each iteration as you intended, make it 
close over the loop variable:

    say ([o] do for 1..2 { $_ + 1/* })(Inf);  # 1.5

TL;DR: Working as intended.

Closing this ticket.

Reply via email to