File: demo_memo.pl

package info (click to toggle)
libhook-lexwrap-perl 0.20-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 68 kB
  • ctags: 30
  • sloc: perl: 274; makefile: 43
file content (19 lines) | stat: -rwxr-xr-x 313 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
use Hook::LexWrap;

sub fibonacci {
	my ($n) = @_;
	return 1 if $n < 3;
	return fibonacci($n-1) + fibonacci($n-2);
}

MEMOIZE: {
	my %cache;
	wrap fibonacci,
		pre  => sub { $_[-1] = $cache{$_[0]} if $cache{$_[0]} },
		post => sub { $cache{$_[0]} = $_[-1] };
}

while (<>) {
	chomp;
	print fibonacci($_), "\n";
}