Орлин обнови решението на 12.03.2013 15:31 (преди над 11 години)
+from collections import OrderedDict, defaultdict
+
+
+def cache(func, cache_size):
+ cache = OrederedDict()
+
+ def func_cached(*args):
+ if args not in cach_vault:
+ if len(cache) == cach_size:
+ cache.popitem(last=False)
+ cache[args] = func(*args)
+ return cache[args]
+
+ return func_cached
+
+def zip_with(func, *iterables):
+ return (func(*args) for args in zip(*iterables)
+
+def iterate(func):
+ current_function = lambda x: x
+ while True:
+ yield current_function
+ current_function = lambda x: func(current_function(x))
+
+def group_by(func, seq):
+ groups = {}
+ for argument in seq:
+ functional = func(argument)
+ if functional not in groups:
+ groups[functional] = []
+ groups[functional].append(argument)
+ return groups