- Example showing data flow synchronisation factilities:
#!/usr/bin/python
import sys
sys.exit(not True in (
bool(int(line)) for line in sys.stdin
)
)
- Same thing refactored into more functional units
#!/usr/bin/python
import sys
def imap(func, iter): #in itertools
for i in iter:
yield func(i) #don't process all input
def any(iter): #in python 2.5
return True in imap(bool,iter)
sys.exit(not any(int(line) for line in sys.stdin))
- Compare yes 0 | head | any || echo none and yes 1 | any && echo some
|