first commit
This commit is contained in:
18
perl/Examples/Chap3/memoize
Normal file
18
perl/Examples/Chap3/memoize
Normal file
@@ -0,0 +1,18 @@
|
||||
|
||||
|
||||
###
|
||||
### memoize
|
||||
###
|
||||
|
||||
## Chapter 3 section 5
|
||||
|
||||
sub memoize {
|
||||
my ($func) = @_;
|
||||
my %cache;
|
||||
my $stub = sub {
|
||||
my $key = join ',', @_;
|
||||
$cache{$key} = $func->(@_) unless exists $cache{$key};
|
||||
return $cache{$key};
|
||||
};
|
||||
return $stub;
|
||||
}
|
||||
Reference in New Issue
Block a user