Thursday, April 30, 2009

Schwartzian Transform - Perl 5 vs. Perl 6

This isn't quite news, but it's a cool little bit of code anyway.

Perl 5:


@sorted = map { $_->[0] }
sort { $a->[1] cmp $b->[1] }
map { [$_, foo($_)] }
@unsorted;


Perl 6:


@sorted = @unsorted.sort: { .uc };


I'm willing to claim that Perl 6 makes this a bit more readable, in spite of the smoke, mirrors and curtains.

Read more about the Schwartzian Transform in Wikipedia.

2 comments:

zby said...

The Perl5 version is more general - it uses 'foo' as the transformation - while the Perl6 version uses 'uc'.

bakkushan said...

True, a better example would use something else than .uc.