Skip to content

Instantly share code, notes, and snippets.

@fcostin
Created November 2, 2012 04:12
Show Gist options
  • Save fcostin/3998672 to your computer and use it in GitHub Desktop.
Save fcostin/3998672 to your computer and use it in GitHub Desktop.
enumerated_casemap
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