Not exactly a classic obfuscation, it's even clearly formatted, but fun with closures anyway.
This is my first post in this section, so I'm not sure if it's entirely appropriate, please be kind.
my $p = sub { my $n = 2; my $g = sub { $n++ }; sub { my $r = $g->(); $g = sub { my ($f, $g) = @_; sub { while (1) { my $r = $g->(); return $r if $r % $f; } }; }->($r, $g); $r; }; }->(); while ((my $n = $p->()) < 500) { print "$n\n"; }
It's a prime number generator. It starts with an iterator
$g = sub { $n++ };
which is captured by the closure $p.
each time you call $p it gets the next value $r from $g then replaces $g with a filter that calls the previous $g until it gets a value that is not a multiple of $r; finally $p returns $r.
It's a technique called the "sieve of Eristothenes": having got a prime number eliminate all multiples of it from your input, then the next number you get must also be prime.
-----
Bill H
perl -e 'print sub { "Hello @{[shift]}!\n" }->("World")'

Replies are listed 'Best First'.
Re: fun with closure
by billh (Pilgrim) on Apr 27, 2006 at 21:40 UTC
    Explaination moved to spoiler
    -----
    perl -e 'print sub { "Hello @{[shift]}!\n" }->("World")'