On Mon, Jun 28, 2010 at 17:08, marcos rebelo <[email protected]> wrote:
snip
>> The "code block" in your example is not a "closure".
>>
>> perldoc -q closure
>> http://en.wikipedia.org/wiki/Closure_(computer_science)
>>
>
> The closure in here was on propose, even if in this case isn't a closure.
snip
Why are you purposely using the wrong terminology? The concept of
closures is orthogonal to the concept of sorting. Just to clarify,
even
my @a = sort { $h{$a} <=> $h{$b} } keys %h;
isn't a closure. A closure occurs when a subroutine closes over a
variable (i.e. it gains its own reference to the variable). Variables
that have been closed over will not be garbage collected until the
closure is destroyed (which follows the normal rule that a variable
won't be garbage collected until its reference count drops to 0).
That version of sort uses a code block (not a subroutine), therefore
the reference count of %h is not affected. In order to use a closure
with sort you must use a named or anonymous subroutine that closes
over the variable:
#!/usr/bin/perl
use strict;
use warnings;
use Scalar::Util::Refcount;
my %h = (
a => 1,
b => 2
);
print "before: ", refcount(\%h), "\n";
my @a = sort {
print "in sort code block: ", refcount(\%h), "\n";
$h{$a} <=> $h{$b};
} keys %h;
my $cmp = sub {
print "in sort closure: ", refcount(\%h), "\n";
return $h{$a} <=> $h{$b};
};
my @b = sort $cmp keys %h;
The preceding code prints
before: 2
in sort code block: 2
in sort closure: 3
We can see that the number of references to %h is two before the sorts
(it is two because we have to take an addition reference to pass it to
refcount), that is stays two inside of the sort code block, but that
it jumps to three when the closure is formed.
Calling a code block a closure when it doesn't close over anything is
a diservice to your reader, who will now erroneously believe that a
closure is any code block, named subroutine, or anonymous subroutine.
snip
>> <QUOTE>
>> # faster, but the element order is aleatory
>> sub unique {
>> my %hash;
>> �...@hash{@_} = ();
>> return keys %hash;
>> }
>> </QUOTE>
>>
>> Aleatory is a rather obscure word (I had to look it up) but it does not
>> accurately describe how hashes work, they are not truly random.
>
> For the point of vista of programmer the order is aleatory. May you
> suggest a better word?
snip
Aleatory is a good word for the order of hash keys (but still not
perfect since it still brings in the concept of randomness), the
question is whether it is appropriate for use in a document designed
for beginners. Perl's own documentation says that keys returns the
keys in "apparently random order". I would say "apparently random
order" is better because:
1. a beginner is more likely to understand it
2. the order is not random (except in Perl 5.8.1 and certain
degenerate cases)
3. it keeps your work in line with the Perl documentation
4. aleatory has a connotation of bad luck
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/