> They can be pretty great, especially when combined with the magic op=
> operators that (in essence) know about identity elements. I've done a few
> challenges on the Code Golf Stackexchange site where I wanted an infinite
> sequence like this:
>
> 0, 1, -2, 3, -4, 5, -6, ...
>
> It took me a while to realize this can be expressed in Raku simply as:
>
> { $++ * ($ *= -1) } ... *
>
Hi Sean, that's a neat solution! I seem to have found an oddity though
(whitespace sensitivity). In the REPL:
> say { $++ * ($ *= -1) } ...21
(0 1 -2 3 -4 5 -6 7 -8 9 -10 11 -12 13 -14 15 -16 17 -18 19 -20 21)
> say { $++ * ($ *= -1) } ...^21
(0 1 -2 3 -4 5 -6 7 -8 9 -10 11 -12 13 -14 15 -16 17 -18 19 -20)
> say { $++ * ($ *= -1) } ... 21
(0 1 -2 3 -4 5 -6 7 -8 9 -10 11 -12 13 -14 15 -16 17 -18 19 -20 21)
> say { $++ * ($ *= -1) } ... ^21
(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20)
I would have expected the last line of code (whitespace between "..."
and "^21") to give the same answer as the first three. Any ideas?
Best, Bill.