Created
November 2, 2012 04:12
-
-
Save fcostin/3998672 to your computer and use it in GitHub Desktop.
enumerated_casemap
This file contains hidden or 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
def enumerated_casemap(case_handlers, things): | |
i = 0 | |
for x in things: | |
for p, f in case_handlers: | |
if p(x): | |
yield f(i, x) | |
i += 1 | |
break | |
def main(): | |
""" | |
example output: | |
['foo', 'fuzz_1_barr', 'foo', 'fuzz_3_barr', 'fuzz_4_barr', 'foo'] | |
""" | |
foo = 'foo' | |
barr = 'barr' | |
bazz = 'bazz' | |
is_foo = lambda x : x == 'foo' | |
is_barr = lambda x : x == 'barr' | |
things = [foo, barr, bazz, bazz, foo, barr, barr, bazz, foo] | |
resulting_things = enumerated_casemap([ | |
(is_foo, lambda i, foo : foo), | |
(is_barr, lambda i, x : 'fuzz_%d_%s' % (i, str(x))), | |
], things) | |
print list(resulting_things) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment