Created
October 18, 2009 17:06
-
-
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.
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
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