Skip to content

Instantly share code, notes, and snippets.

@davips
Created May 27, 2020 07:49
Show Gist options
  • Save davips/eff9cebb86436e99b173f8e19f9cc05b to your computer and use it in GitHub Desktop.
Save davips/eff9cebb86436e99b173f8e19f9cc05b to your computer and use it in GitHub Desktop.
collection iterator working!
from functools import lru_cache
from itertools import repeat
def pca(d):
return d * 2
def svm(d):
return d * 1000
def rf(d):
return d * 88
def knn(d):
return d * 3333
class Collection:
def __init__(self, generator, finalizer):
self.generator = generator
self.finalizer = finalizer
self._data = None
self._last_args = None
def __iter__(self):
return self
def __next__(self):
data = next(self.generator)
if type(data) == tuple:
data, *self._last_args = data
return data
@property
@lru_cache()
def data(self):
if self._last_args is None:
raise Exception('Data object not ready!')
return self.finalizer(*self._last_args)
def expand(data):
generator = repeat(data)
return Collection(generator, lambda: data)
def partition(data):
generator = iter(range(4))
return Collection(generator, lambda: data)
def map_(collection, f):
generator = map(f, collection)
return Collection(generator, lambda: collection.data)
def multi(collection, fs):
generator = map(lambda f, x: f(x), fs, collection)
return Collection(generator, lambda: collection.data)
def summ(collection):
def generator():
res = 1
for data in collection:
res += data
yield data, res
return Collection(generator(), lambda res: res)
def reduce(collection):
# Exhaust iterator.
for _ in collection:
pass
return collection.data
d = 2
d2 = reduce(
summ(
multi(
partition(d),
[svm, rf, knn, pca]
)
)
)
print(d2)
print()
print('-------------------------------------')
d2 = reduce(
summ(
multi(
map_(
partition(d),
pca
),
[svm, rf, knn, pca]
)
)
)
print(d2)
exit()
# def partition(d):
# for cc in range(7):
# yield cc
# yield None # None marca que o próximo é o pendurado.
# yield d
#
#
# def map(c, f):
# for d in c:
# r = f(d)
# print('map gerou', r)
# if d is None: # é bom checar se loop é infinito e dar hint.
# break
# yield r
# yield None
# yield next(c)
#
# # multi vai ignorar datas excedentes!
#
#
# def multi(c, fs):
# for f in fs: # tratar StopException com hint sobre pipeline?
# d = next(c)
# if d is None:
# raise Exception('Less Data objects than expected!')
# r = f(d)
# print(' multi gerou', r)
# yield r
# yield None
# yield next(c)
#
#
# def summ(c):
# res = 0
# for d in c:
# if d is None:
# break
# res *= d
# yield d
# yield None
# yield res
#
#
# # Reduce saiu p/ bater c/ o Expand e pq pode não ser aplicado no prior.
# # def summ(prior_, posterior_):
# # for d in posterior_:
# # d += 1
# # return prior_, posterior_
#
#
# def reduce(c):
# for d in c:
# if d is None:
# break
# return next(c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment