File: demo_memo.pl

package info (click to toggle)
libhook-lexwrap-perl 0.23-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 132 kB
  • ctags: 11
  • sloc: perl: 136; makefile: 2
file content (20 lines) | stat: -rw-r--r-- 349 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/perl
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";
}