r/perl6 • u/liztormato • Nov 06 '18
r/perl6 • u/aaronsherman • Jul 24 '16
A Python guy in Perl 6 object land
I used to be a Perl programmer, professionally, but that was in the '90s. Today I'm almost exclusively locked into Python 2 for my day job, and I've been a "Python guy" for years now.
In using Perl 6, one of the things that constantly floors me is how elegant objects are, and one of the most important elements of that elegance is the far-reaching impact of the "just do the right thing" attitude that class construction has.
When defining a class like:
class File {
has Int $.size is rw;
has Str $.owner;
}You get so much for free! The constructor already exists, so you can do this:
my $file = File.new(:size(1_000), :owner("bob"));There are inheritable accessors built on top of the attributes that I defined, so I can call those in any derived class without having to reach in and mess with private data directly:
class ImageFile {
has $.width;
has $.height;
has $.bytes_per_pixel = 4;
method image_dims(:$!width, :$!height) {
self.size = self.width * self.height * self.bytes_per_pixel;
}
}C++ is, I think, the language that introduced the idea of a method parameter that directly set attributes, but Perl 6 already had a syntax for this in a general purpose way. When you pass variables to a function by name, you can pass them with their own name: foo(:$bar, :$baz). Because of this, the syntax for "accept named parameters that set properties" is obvious. The private variable that corresponds to a public attribute is prefixed with $! so :$! means "the named parameter that corresponds to the attribute."
It's not really that I want shorter code... that would be just capricious golfing. No, I want, and love in Perl 6, the ability to eliminate code that exists only to tell the compiler to do what the human reader already knows I want to do. I shouldn't have to say, "this method takes an instance variable parameter," or, "now set this attribute equal to its correspondingly named parameter." Yet, in Python, I'm constantly writing:
def __init__(self, a, b):
self.a = a
self.b = bThis feels like Perl 5 where subroutines "could handle parameter passing" in that you could manually manage the stack:
sub foo {
my $a = shift @_;
my $b = shift @_;
...
}I still love Python. When it suits the task I'm working on, it suits it like a glove! I can make Perl 6 suit the same tasks, but it's often work. Yet, the domain over which Perl 6 suits tasks well is so much larger than with any other language I've used! Slinging parsers and metaobjects and curried closures and lazy map/reductions as first-class features is such a joy! Many of these things exist in Python too. Some are easy to use (metaobjects) some ... are usable (lazy map/reductions) and some just don't exist (first class parsers).
r/perl6 • u/tbrowder • Mar 18 '19
I'm admiring my brand new Florida US Perl 6 vehicle tag!
r/perl6 • u/hankache • Dec 24 '15
An Unexpectedly Long-expected Party
r/perl6 • u/OvidPerl • Oct 11 '19
Larry has stated he's in favor of renaming Perl 6 to raku
r/perl6 • u/MattEOates • Jun 21 '16
On The Shoulders Of Giants - Damian Conway
r/perl6 • u/liztormato • May 23 '19
Why I love Perl 6 | Damian Conway [blogs.perl.org]
blogs.perl.orgr/perl6 • u/zoffix • Jul 27 '18
6.d spec contains over 3,400 commits, made by more than 60 authors
r/perl6 • u/CrazyM4n • Apr 05 '19
A Whirlwind Tour of Perl6’s Best Features
aearnus.github.ior/perl6 • u/zoffix • Oct 24 '18
Perl 6 'Diwali' 6.d: More Well-Defined/Faster Set Operators
r/perl6 • u/aaronsherman • Aug 07 '19
Perl 6's grammars / rules still stun me all these years later...
Just think about this for a second... this is a valid parser for the complete JSON spec in pure Perl, with no external modules or utilities required.
My head still spins...
grammar JSON {
rule TOP {^ <value> $}
rule value {
<object> | <array> | <number> | <string> | <name>
}
rule name { <true> | <false> | <null> }
token true { 'true' }
token false { 'false' }
token null { 'null' }
rule object {
'{' ( <string> ':' <value> )* % ',' '}'
}
rule array {
'[' <value>* % ',' ']'
}
token number {
'-'?
[ '0' | <[1..9]> <digit>* ]
[ '.' <digit>+ ]?
[:i <[e]> <[\+\-]>? <digit>+ ]?
}
token digit { <[0..9]> }
token string {
'"' ~ '"' <stringbody>*
}
token stringbody {
# anything non-special:
<-[\"\\\x[0000]\x[001f]]> |
# or a valid escape
'\\' <escape>
}
token escape {
# An escaped special of some sort
<[\"\\\/bfnr]> |
# or a Unicode codepoint
'u' [:i <+[0..9]+[a..f]>] ** 4
}
}I wrote the first draft of the Wikipedia page on Perl 6 Rules. I'm certainly not new to them. But every time I go back and play with them, I'm floored and at the same time mad as hell that every language in the world isn't using them.
Edit: changed confusingly named <literal> to <name>. In the spec, they're called "literal names" hence my confusion. They're not actually literals, but the name is literally matched.
r/perl6 • u/steve_mynott • Nov 06 '18
Larry Wall Announces Alternative Name “Raku” for Perl 6
r/perl6 • u/zoffix • Nov 05 '18
Announce: Raku Perl 6 'Diwali' 6.d Language Specification Release
r/perl6 • u/javan_z • Dec 11 '17
Perl6 being used at NASA?
https://github.com/CurtTilmes/perl6-eredis
High Performance bindings for Redis
r/perl6 • u/zoffix • Nov 19 '16
Another Perl 6 Book in the Works: "Perl 6 By Example"
r/perl6 • u/aaronsherman • Aug 16 '16
A Python guy in Perl 6 land part 2: itertools
I absolutely love itertools in Python, so in exploring Perl 6, one of the most important things for me is making sure that I'm not missing something there. So, what does itertools look like in P6?
It turns out, it's mostly builtins. The infinite iterators, for example, are just lists in Perl 6 (all of which are lazy unless you tell them not to be):
^Inf # 0 to infinity
1, *+2...* # The positive, odd integers
|<A B C D> xx * # A cycle of four string values, A, B, C, D, A, B...The next section of itertools are more or less just filters and flatteners. For example:
(^Inf).grep: {.is-prime} # All prime numbers
|@a, |@b # the contents of a and b as a single iterableThese seeming lists can be subscripted and sliced just like an Array:
$ perl6 -e 'say ((^Inf).grep: {.is-prime})[^10]'
(2 3 5 7 11 13 17 19 23 29)This is because laziness isn't just a temporary state. If you ask for the tenth item in a lazy list, then ten items are requested. If the list is infinitely long, that doesn't matter.
In the docs, the next section is the combinatoric generators. These are built into Perl 6 iterable objects as methods:
<a b c>.permutations # a, b, c; a, c, b; etc.
@a X @b # Cartesian productBut because Perl 6 just does everything lazily, you don't have to wait around for someone to write an itertools function to help you out or write it yourself. You just do what you need to do with a list and the right thing generally happens.
But if you want itertools, you can write it yourself pretty easily. Here's how it might start:
use v6;
unit module Python::Itertools;
sub count($start, $step=1) { $start, *+$step ... * }
sub cycle(*@iterables) { |@iterables xx * }
sub repeat($elem, $n=Inf) { $elem xx $n }
sub accumulate(*@iterables) { [\+] @iterables }
sub chain(*@iterables) { @iterables }
sub compress(@data, @selectors) {
zip(@data, @selectors).grep({.[1]}).map: {.[0]}
}
...