Thursday, April 12, 2012

[mvwshaxw] Reserving identifiers in a scope

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).


Cetin Sert said...

Mathematica has #-prefixed numbered arguments in its pure functions. (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

Anonymous said...

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 for more information.

Anonymous said...

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.

Anonymous said...