On Thu, Mar 22, 2012 at 9:07 AM, Bruce Gray <bruce.g...@acm.org> wrote:
> On Mar 21, 2012, at 11:49 PM, Jonathan Lang wrote: > > What I want to know is whether there's a way to define a step function >> that's based in part or in whole on the current term's index. For example, >> how would I use infix:<...> to generate the perfect squares between 0 and >> 100? Namely, '0,1,4,9,16,25,36,49,64,81,**100'. For example, is Perl 6 >> set up to try to pass the current element's index into the step function by >> means of a named parameter? >> >> I would hope for something like: >> >> -> :index { $index * $index } ... 100 >> >> or >> >> 1, *+:index ... * # 1,3,6,10,15,21,28,36,45,... >> >> (Note: I _don't_ expect the above to work as is; they're merely intended >> to convey a rough idea of what I _do_ want.) >> >> If not, how _would_ I generate these lists? >> > > > In real life, you would just use this: > my @squares = map { $_ * $_ }, 0..10; > or, for an infinite list, use a binding instead of an assignment: > my @squares := map { $_ * $_ }, 0..*; > True enough. > But you were asking about ... specifically :^) > Yes, I was. > On freenode/#perl6, I was pointed to part of the spec that I had > overlooked. > The "sequence operator" is defined in S03: > > http://perlcabal.org/syn/S03.**html#List_infix_precedence<http://perlcabal.org/syn/S03.html#List_infix_precedence> > Buried in its definition is this gem: > > http://perlcabal.org/syn/S03.**html#line_1884<http://perlcabal.org/syn/S03.html#line_1884> > The function may also be slurpy (n-ary), in which case all > the preceding values are passed in (which means they must > all be cached by the operator, so performance may suffer, > and you may find yourself with a "space leak"). > Interesting; it never occurred to me to try to retrieve the current index by slurping in all of the previous elements and counting them. > After writing all the above, it occurred to me that the use of @_ should > implicitly define a closure as slurpy/n-ary. That would remove the need > for the arrow, and make the code much less ugly. > Also, the first value (0) might be unnecessary. The spec says that it > should not be required when the closure is 0-ary, but I think that > should also be true for slurpy/n-ary closures. > Agreed: starting values should only be mandatory when dealing with a step function that definitely requires them. > These work in Niecza: > > my @squares := { @_ ** 2 } ... *; > And this is rather elegant, syntactically (though not so much in the performance department). -- Jonathan "Dataweaver" Lang