Skip to content

Instantly share code, notes, and snippets.

@masak
Created August 16, 2011 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masak/1149245 to your computer and use it in GitHub Desktop.
Save masak/1149245 to your computer and use it in GitHub Desktop.
Perl 6 makes code look nicer
Perl 5 version:
use constant INFINITY => 2 ** 32;
sub choose_column {
my ($self) = @_;
my $r = $self->linked_matrix;
my $column_with_minimal_ones;
my $minimal_ones = INFINITY;
for (my $h = $r->R; $h ne $r; $h = $h->R) {
if ($minimal_ones > $h->S) {
$minimal_ones = $h->S;
$column_with_minimal_ones = $h;
}
}
return $column_with_minimal_ones;
}
Perl 6 version:
method choose-column() {
my $r = self.linked-matrix;
return min :by(*.S), ($r.R, *.R ...^ $r);
}
'Nuff said.
@soh-cah-toa
Copy link

Porting Perl 5 code to Perl 6 (a task I've been doing lately with IRC::Utils) really makes you aware just how superior Perl 6 is to Perl 5. The only area I think Perl 5 takes the cake is documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment