Марио обнови решението на 15.03.2013 22:15 (преди над 11 години)
+from collections import deque
+import itertools
+
+
+def groupby(func, seq):
+ function_map = dict()
+ for i in seq:
+ res = func(i)
+ if res not in function_map:
+ function_map[res] = []
+ function_map[res].append(i)
+ return function_map
+
+
+def iterate(func):
+ def identity(*args):
+ if len(args) == 1:
+ return args[0]
+ else:
+ return args
+
+ for i in itertools.count():
+ def composed_func(*args):
+ if i == 0:
+ return identity(*args)
+ else:
+ res = args
+ for x in range(i):
+ if x == 0:
+ res = func(*args)
+ else:
+ res = func(res)
+ return res
+ yield composed_func
+
+
+def zip_with(func, *iterables):
+ size = -1
+ for iterable in iterables:
+ iterable_size = len(iterable)
+ if iterable_size < size or size == -1:
+ size = iterable_size
+
+ for i in range(size):
+ list = []
+ for iterable in iterables:
+ list.append(iterable[i])
+ yield func(*list)
+
+
+def cache(func, cache_size):
+ values_dict = dict()
+ values_order = deque()
+
+ def cached_func(*args):
+ if args not in values_dict:
+ if len(values_order) >= cache_size:
+ last_element = values_order.pop()
+ del values_dict[last_element]
+ values_order.appendleft(args)
+ values_dict[args] = func(*args)
+ return values_dict[args]
+
+ return cached_func