Skip to content

Instantly share code, notes, and snippets.

@yetone
Forked from kxxoling/pipe.py
Last active August 29, 2015 14:15
Show Gist options
  • Save yetone/e2b96f4813499c9c0ba0 to your computer and use it in GitHub Desktop.
Save yetone/e2b96f4813499c9c0ba0 to your computer and use it in GitHub Desktop.
class Pipe(object):
def __init__(self, func):
self.func = func
def __ror__(self, other):
def generator():
for obj in other:
if obj is not None:
yield self.func(obj)
return generator()
@Pipe
def even_filter(num):
return num if num % 2 == 0 else None
@Pipe
def multiply_by_three(num):
return num*3
@Pipe
def convert_to_string(num):
return 'The Number: %s' % num
@Pipe
def echo(item):
print item
return item
def force(sqs):
for item in sqs: pass
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
force(nums | even_filter | multiply_by_three | convert_to_string | echo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment