Емил обнови решението на 15.03.2013 21:21 (преди над 11 години)
+#groupby
+
+def groupby(func, seq):
+ result = {}
+ for element in seq:
+ key = func(element)
+ if result.__contains__(key):
+ result[key].append(element)
+ else:
+ result[key] = [element]
+ return result
+
+
+#iterate
+
+from itertools import count
+
+l = ['dummy']
+
+def set_list(l, func):
+ old_func = l[0]
+ new_func = lambda *arg: func(old_func(*arg))
+ l[0] = new_func
+ return old_func
+
+def iterate(func):
+ global l
+ l[0] = lambda x: x
+ return (set_list(l,func) for i in count(0))
+
+#zip_with
+
+def zip_with(func, *iterables):
+ if (len(iterables) != 0 ):
+ min_len = min(map(len, iterables))
+ result = []
+ for i in range(min_len):
+ list_ = []
+ for element in iterables:
+ list_.append(element[i])
+ arg = tuple(list_)
+ result.append(func(*arg))
+ return result
+ else:
+ return []
+
+#cache
+
+def cache(func, cache_size):
+ def func_cached(x, l=[]):
+ dictionary = dict(l)
+ if x in dictionary:
+ return dictionary[x]
+ else:
+ value = func(x)
+ l.append((x,value))
+ if len(l) >= cache_size + 1:
+ l.pop(0)
+ return value
+ return func_cached
+
+
+