Created with <3 with dartpad.dev.
Created
October 3, 2023 12:18
-
-
Save wonderkidshihab/74012490ea0e1e5b4d2d496b5bce9f60 to your computer and use it in GitHub Desktop.
crimson-cliff-8989
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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