On Thu, Jul 22, 2010 at 11:35 AM, Aaron Sherman <a...@ajs.com> wrote:
> On Thu, Jul 22, 2010 at 1:13 PM, Jon Lang <datawea...@gmail.com> wrote:
>>  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.

How so?

> 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.

...which differs from my example in several ways, all of which are
detrimental: it puts the index in among the positional parameters,
meaning that odd things would have to happen if you ever decide to
use, say, $^i and $^j as your "prior items" parameters; and it locks
you into a specific name for the index instead of letting you choose
one of your own liking.

> 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.

Who said anything about changing the syntax?  "$:i" is perfectly valid
syntax that is already spelled out in S06, under Placeholder
Variables.  Granted, it's rarely used; but it exists.  And unless I'm
missing something, this would be a perfectly valid use for it.

Essentially, my suggestion is this: if the step function's signature
(or implied signature, in the case of a function with placeholder
variables) includes any named parameters, then the index is used as
the argument corresponding to the first one.  (the only catch would be
if you decide to use the slurpy named placeholder, since the compiler
can't be expected to know which keys are being used inside the block;
in this case, it would be fair to assume that "index" is to be used,
or maybe "0".)  As such, the above examples could also be done as:

    0, -> $a, :$i { $a + $i } ... * # series of triangle numbers
    0, sub ($_, :$x) { $_ + (2 * $x - 1)  } ... * # series of square numbers
    { %_{"0"} ** 2 } ... * # series of square numbers
    1, { @_[0] * %_<0> } ... * # series of factorials

My own preference would be to angle for the more compact form that I
originally illustrated, unless and until its limitations force me to
do otherwise.  But then, TIMTOWTDI.

-- 
Jonathan "Dataweaver" Lang

Reply via email to