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.