Front page | perl.perl6.language |
Postings from May 2009
Re: Amazing Perl 6
From: Damian Conway
May 28, 2009 03:38
Re: Amazing Perl 6
Message ID: email@example.com
Daniel Carrera wrote:
> This is a really good list. Mind if I copy it / modify it and post it
> somewhere like my blog?
> One question:
>> * Compactness of expression + semi-infinite data structures:
>> @fib = 1,1...&[+] # The entire Fibonacci sequence
> Very impressive. That's even shorter than Haskell. What's &[+] ? How does
> this work?
The ... operator takes a list on the left and a generator sub (or block)
on the right. It creates a lazy list consisting initially of the left
operand. When the left operand runs out, the right operand is invoked to
generate extra values. The generator grabs as many existing values from
the end of the list as it needs arguments, then appends its result to the
end of the lazy list. Lather, rinse, repeat, as often as necessary.
In the Fibonacci example, the generator sub is &[+], which is a
shorthand for &infix:<+>. So, after 1,1 the generator grabs the last
two values (i.e. 1,1), adds them with &infix:<+>, and puts 2 on the end
of the list. Next time a value is needed, &infix:<+> grabs the final
two list elements (now: 1,2), adds them, and appends 3 to the list.
Et cetera, et cetera.
For the use of ... to generate lists see: "infix:<...>, the series operator"
under http://perlcabal.org/syn/S03.html#List_infix_precedence. For the
&[+] syntax, see http://perlcabal.org/syn/S03.html#Nesting_of_metaoperators
>> PS: A really mean, but very effective, way of emphasizing the ease,
>> expressiveness, compactness, and readability of Perl 6 code is to
>> take each of the examples and show the same thing written in Java. >;-)
> It might be appropriate to compare some examples with Ruby or Python.
Only if the comparisons are suitably invidious. ;-)