Skip to content

Instantly share code, notes, and snippets.

@sshirokov
Created October 18, 2009 17:06
Show Gist options
  • Save sshirokov/212752 to your computer and use it in GitHub Desktop.
Save sshirokov/212752 to your computer and use it in GitHub Desktop.
Helper to map a chain of functions in order to a given list.
def mapchain(*functions_then_list):
'''
mapchain(f..fn, list)
Map the functions in sequence from left to right across list
mapchain(f1, f2, f3, f4, list) => map(f4, map(f3, map(f2, map(f1, list))))
'''
if len(functions_then_list) < 2: raise TypeError("mapchain(f..fn, list) requires at least one function and a list")
ops, items = functions_then_list[0:-1], functions_then_list[-1]
if not reduce(lambda a, b: a and b, map(callable, ops), True): raise TypeError("All but the list must be callable")
return reduce(lambda a, b:\
map(b, a),
ops,
items)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment