summaryrefslogtreecommitdiff
path: root/lib/leftpadesque/memoize.ts
diff options
context:
space:
mode:
authorElizabeth Hunt <me@liz.coffee>2025-07-27 17:03:10 -0700
committerElizabeth Hunt <me@liz.coffee>2025-07-27 18:30:30 -0700
commit9970036d203ba2d0a46b35ba6fad21d49441cdd4 (patch)
treea585d13933bf4149dcb07e28526063d071453105 /lib/leftpadesque/memoize.ts
downloadpengueno-9970036d203ba2d0a46b35ba6fad21d49441cdd4.tar.gz
pengueno-9970036d203ba2d0a46b35ba6fad21d49441cdd4.zip
hai
Diffstat (limited to 'lib/leftpadesque/memoize.ts')
-rw-r--r--lib/leftpadesque/memoize.ts14
1 files changed, 14 insertions, 0 deletions
diff --git a/lib/leftpadesque/memoize.ts b/lib/leftpadesque/memoize.ts
new file mode 100644
index 0000000..2f0e87a
--- /dev/null
+++ b/lib/leftpadesque/memoize.ts
@@ -0,0 +1,14 @@
+import type { Callable } from '@emprespresso/pengueno';
+
+export const memoize = <R, F extends Callable<R>>(fn: F): F => {
+ const cache = new Map<string, R>();
+ return ((...args: unknown[]): R => {
+ const key = JSON.stringify(args);
+ if (cache.has(key)) {
+ return cache.get(key)!;
+ }
+ const res = fn(...args);
+ cache.set(key, res);
+ return res;
+ }) as F;
+};