Красимира обнови решението на 14.03.2013 00:38 (преди над 11 години)
+from collections import OrderedDict
+
+
+def groupby(func, seq):
+ dictionary = {}
+ for x in seq:
+ dictionary.setdefault(func(x), []).append(x)
+ return dictionary
+
+
+def iterate(func):
+ def generate_func(iter_func):
+ return lambda x: func(iter_func(x))
+ iter_func = lambda x: x
+ while True:
+ yield iter_func
+ iter_func = generate_func(iter_func)
+
+
+def zip_with(func, *iterables):
+ if (len(iterables)):
+ min_length = min([len(x) for x in iterables])
+ for i in range(min_length):
+ yield func(*[x[i] for x in iterables])
+
+
+def cache(func, cache_size):
+ cache_dict = OrderedDict()
+
+ def cache_memory(*args):
+ if args in cache_dict:
+ return cache_dict[args]
+
+ if len(cache_dict) >= cache_size:
+ cache_dict.popitem(last=False)
+ first_func_call = func(*args)
+ cache_dict[args] = first_func_call
+ return first_func_call
+
+ return cache_memory