Skip to content

Instantly share code, notes, and snippets.

@wonderkidshihab
Created October 3, 2023 12:18
Show Gist options
  • Save wonderkidshihab/74012490ea0e1e5b4d2d496b5bce9f60 to your computer and use it in GitHub Desktop.
Save wonderkidshihab/74012490ea0e1e5b4d2d496b5bce9f60 to your computer and use it in GitHub Desktop.
crimson-cliff-8989
int callCount = 0;
void main(){
var memFibo = memoize(input);
print(callCount);
print(memFibo(20, 2));
print(callCount);
print(memFibo(25, 5));
print(callCount);
print(memFibo(20,2));
print(callCount);
}
Function(int, int) memoize(Function(int, int) fn) {
final cache = <int, int>{};
return (int n, int m) {
if (cache.containsKey(n)) {
return cache[n]!;
}
final result = fn(n, m);
cache[n] = result;
return result;
};
}
int input(int n, int m) {
callCount++;
return n + m;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment