Skip to content

Instantly share code, notes, and snippets.

@dunossauro
Last active January 16, 2017 08:14
Show Gist options
  • Save dunossauro/4bf85182b599a605234facfb758942d8 to your computer and use it in GitHub Desktop.
Save dunossauro/4bf85182b599a605234facfb758942d8 to your computer and use it in GitHub Desktop.
Implementation of a simple stream
"""
Stream to functions
"""
from os import listdir
def read_file(arq):
with open(arq) as _file:
return _file.read()
def double(vals):
return [x*2 for x in vals]
def concat(vals):
return ''.join([read_file(val) for val in vals])
class Stream:
def __init__(self):
self.pipes = []
self.values = []
self.last_result = False
def source(self, vals):
for val in vals:
self.values.append(val)
return self
def pipe(self, func):
self.pipes.append(func)
return self
def execute(self):
for pipe in self.pipes:
if not self.last_result:
self.last_result = pipe(self.values)
else:
self.last_result = pipe(self.last_result)
return self.last_result
stream_1 = Stream()
teste_1 = stream_1.source(listdir('.'))\
.pipe(concat)\
.execute()
stream_2 = Stream()
teste_2 = stream_2.source([1, 2, 3, 4, 5])\
.pipe(double)\
.pipe(double)\
.pipe(double)\
.execute()
print(teste_1)
print(teste_2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment