|
@@ -1,13 +1,3 @@
|
|
|
-# def cache(f):
|
|
|
|
|
-# def wrapper(*args):
|
|
|
|
|
-# if not hasattr(f, '__cache__'):
|
|
|
|
|
-# f.__cache__ = {}
|
|
|
|
|
-# key = args
|
|
|
|
|
-# if key not in f.__cache__:
|
|
|
|
|
-# f.__cache__[key] = f(*args)
|
|
|
|
|
-# return f.__cache__[key]
|
|
|
|
|
-# return wrapper
|
|
|
|
|
-
|
|
|
|
|
class cache:
|
|
class cache:
|
|
|
def __init__(self, f):
|
|
def __init__(self, f):
|
|
|
self.f = f
|
|
self.f = f
|
|
@@ -16,4 +6,29 @@ class cache:
|
|
|
def __call__(self, *args):
|
|
def __call__(self, *args):
|
|
|
if args not in self.cache:
|
|
if args not in self.cache:
|
|
|
self.cache[args] = self.f(*args)
|
|
self.cache[args] = self.f(*args)
|
|
|
- return self.cache[args]
|
|
|
|
|
|
|
+ return self.cache[args]
|
|
|
|
|
+
|
|
|
|
|
+def reduce(function, sequence, initial=...):
|
|
|
|
|
+ it = iter(sequence)
|
|
|
|
|
+ if initial is ...:
|
|
|
|
|
+ value = next(it)
|
|
|
|
|
+ if value is StopIteration:
|
|
|
|
|
+ raise TypeError("reduce() of empty iterable with no initial value")
|
|
|
|
|
+ else:
|
|
|
|
|
+ value = initial
|
|
|
|
|
+ for element in it:
|
|
|
|
|
+ value = function(value, element)
|
|
|
|
|
+ return value
|
|
|
|
|
+
|
|
|
|
|
+class partial:
|
|
|
|
|
+ def __init__(self, f, *args, **kwargs):
|
|
|
|
|
+ self.f = f
|
|
|
|
|
+ if not callable(f):
|
|
|
|
|
+ raise TypeError("the first argument must be callable")
|
|
|
|
|
+ self.args = args
|
|
|
|
|
+ self.kwargs = kwargs
|
|
|
|
|
+
|
|
|
|
|
+ def __call__(self, *args, **kwargs):
|
|
|
|
|
+ kwargs.update(self.kwargs)
|
|
|
|
|
+ return self.f(*self.args, *args, **kwargs)
|
|
|
|
|
+
|