Last active
December 14, 2015 17:59
-
-
Save P4/5125801 to your computer and use it in GitHub Desktop.
Re-implement Python's for statement using @decorator syntax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python | |
def foreach(iterable): | |
def iterate_over(func): | |
iterator = iter(iterable) | |
while True: | |
try: | |
value=next(iterator) | |
func(value) | |
except StopIteration: | |
break | |
return func # your function can be used later as if nothing happened | |
return iterate_over | |
if __name__ == '__main__': | |
a = list(range(15)) | |
@foreach(a) | |
def thing(i): | |
print(i) | |
# equivalent to | |
# for i in a: | |
# print(i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
wat