On Thu, Jul 22, 2010 at 1:13 PM, Jon Lang <[email protected]> wrote:
> > I also think it's doable without a special tool:
> >
> > 0, { state $i = 1; $^a + $i++ } ... *
>
> Kludgey; but possibly doable.
>
Well, it's kind of what state is there for.
>
> But what I'd really like to see would be for the index to be passed
> into the step function via a named parameter.
Of course, you say "the index" as if there is such a thing. In reality,
there's no reason for the series operator to keep an index unless it's
explicitly being indexed (e.g. by postcircumfix:<[]>)
> Yes, it would be a
> special tool; but it would be much more in keeping with the "keep
> simple things easy" philosophy that Perl 6 tends to promote:
>
> 0, { $^a + $:i } ... * # series of triangle numbers
> 0, { $^a + (2 * $:i - 1) } ... * # series of square numbers
> { $:i ** 2 } ... * # series of square numbers
> 1, { $^a * $:i } ... * # series of factorials
I do have to admit that that's awfully clean-looking, but the implementation
would force a closure in a series to behave differently from a closure
anywhere else.
Without changing closure definitions and without extending the syntax any,
you could make the series operator do a little bit more introspection work
and if a parameter is named "index", track an index value and pass it by
name, passing any remaining parameters positionally from the previous n
values as normal.
That makes your examples:
0, { $^a + $^index } ... *
0, { $^a + (2 * $^index - 1) } ... *
{ $^index ** 2 } ... *
1, { $^a * $^index } ... *
Not changing the syntax of closures seems like a reasonable goal at this
late stage.
--
Aaron Sherman
Email or GTalk: [email protected]
http://www.ajs.com/~ajs