The Perl sort function is special, optionally taking a comparison block within which the variables a and b are special. E.g., sort { $hash{$a} <=> $hash{$b} } (keys %hash)
.
Create a systematic or general-purpose mechanism for this, not necessarily for Perl. The idea is a lambda, but not having to declare the arguments to the lambda function: they are already chosen for you. The goal is to type less (at the cost of having to memorize what the special argument names are).
4 comments :
Mathematica has #-prefixed numbered arguments in its pure functions. http://reference.wolfram.com/mathematica/tutorial/PureFunctions.html (third one from above, also called short form pure functions). The thing is they auto-compile better than the longer notations and are highly encouraged if you want to write high-performance code.s
Perl 6 uses "self-declared positional variables" for this:
sort { %hash{$^a} <=> %hash{$^b} } keys %hash
The variables $^a and $^b are local to the block and are implicitly ordered by the Unicode ordering of their names. For example,
sort { %hash{$^first} <=> %hash{$^last} } keys %hash
would work just as well, because 'f' < 'l'. This is general purpose syntax for blocks, for example you could declare a lambda function by
my $fun = { 2 * $^x * $^y };
with would have the same effect as
my $fun = -> $x, $y { 2 * $x * $y };
If interested, see http://perl6advent.wordpress.com/2010/12/23/day-23-its-some-sort-of-wonderful/ for more information.
If your goal is to type less, use a better editor, like one that is aware of programming language syntax, or at least has macros.
If your goal is to have the source files take less disk space, zip them.
Haskell: http://hpaste.org/67309
Post a Comment