aboutsummaryrefslogtreecommitdiff
path: root/etc
diff options
context:
space:
mode:
authorGravatar Jason Gross <jgross@mit.edu>2017-08-11 16:38:49 -0400
committerGravatar Jason Gross <jgross@mit.edu>2017-08-11 16:39:14 -0400
commit98148416eb49a9b327175c1a3103ef4f0f977a0d (patch)
tree3927080d320b13aff9eca96653f238858ac6a1c7 /etc
parent167d1e1adc236880e171c04063d55d1ea0041d21 (diff)
Add memoize.py to zinc compiler folder
Diffstat (limited to 'etc')
-rw-r--r--etc/compile-by-zinc/memoize.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/etc/compile-by-zinc/memoize.py b/etc/compile-by-zinc/memoize.py
new file mode 100644
index 000000000..d5908390d
--- /dev/null
+++ b/etc/compile-by-zinc/memoize.py
@@ -0,0 +1,17 @@
+
+# from http://code.activestate.com/recipes/578231-probably-the-fastest-memoization-decorator-in-the-/
+
+__all__ = ["memoize"]
+
+def memoize(f):
+ """ Memoization decorator for a function taking one or more arguments. """
+ class memodict(dict):
+ def __getitem__(self, *key, **kwkey):
+ return dict.__getitem__(self, (tuple(key), tuple((k, kwkey[k]) for k in sorted(kwkey.keys()))))
+
+ def __missing__(self, key):
+ args, kwargs = key
+ self[key] = ret = f(*args, **dict(kwargs))
+ return ret
+
+ return memodict().__getitem__