« | perl -0777 -pe 's/xxx/yyy/g' | Home | Running in the compile time »

August 23, 2009

Hash::Lazy

Just shipped Hash::Lazy, a spiffy way to let you build a hash lazily.

Here’s the way to recursively define a fibonacci series with it:

my $fib = Hash { my ($h, $k)= @_; return $h->{$k-1} + $h->{$k-2} };
$fib->{0} = 0;
$fib->{1} = 1;

The latter 2 statements sets the first 2 seed values of the series. Expressions like $fib->{10} will be calculate on-demand, recursively, memoized.

Interestingly, this kind of code feels a bit like functional programming to me. I guess one can invent a Function keywords that declare functions to do the same.

No TrackBacks

TrackBack URL: http://gugod.org/mt/mt-tb.cgi/237

Leave a comment