Showing posts with label fibers. Show all posts
Showing posts with label fibers. Show all posts

Thursday, January 17, 2008

Don't know if I'm fully following this or understand exactly what a Ruby Fiber is ...

But it did inspire me to come up with this little bit of Python black-magic :



def makeSection(f) :
def gen(source) :
for x in source :
yield f(x)
return gen


def makeFilter(test) :
def gen(source) :
for x in source :
if test(x) : yield x
return gen


def pipeline(generators) :
def p(source) :
old = source
for g in generators :
new = g(old)
old = new
return old
return p

t2 = makeSection(lambda x : x * 2)
t3 = makeSection(lambda x : x * 3 )
f1 = makeFilter(lambda x : (x>20))
f2 = makeFilter(lambda x : (x<50))

pipe = pipeline( [ t2 , t3 , f1 , f2 ] )

for x in pipe(range(10)) : print x



See if you can work it out, I'll probably go over it tomorrow ...