Created
March 26, 2014 21:15
-
-
Save rileyjshaw/9793744 to your computer and use it in GitHub Desktop.
Two pipeline functions: map(reduce(stuff)) and reduce(map(stuff))
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 assoc(_d, key, value): | |
from copy import deepcopy | |
d = deepcopy(_d) | |
d[key] = value | |
return d | |
def set_canada_as_country(band): | |
return assoc(band, 'country', "Canada") | |
def strip_punctuation_from_name(band): | |
return assoc(band, 'name', band['name'].replace('.', '')) | |
def capitalize_names(band): | |
return assoc(band, 'name', band['name'].title()) | |
############################################################# | |
# Pipelines | |
########### | |
def pipeline_each(data, fns): | |
return map(lambda band: reduce(lambda acc, fn: fn(acc), | |
fns, | |
band), | |
data) | |
def pipeline_each_supercool(data, fns): | |
return reduce(lambda acc, x: map(x, acc), | |
fns, | |
data) | |
############################################################# | |
bands = [{'name': 'sunset rubdown', 'country': 'UK', 'active': False}, | |
{'name': 'women', 'country': 'Germany', 'active': False}, | |
{'name': 'a silver mt. zion', 'country': 'Spain', 'active': True}] | |
print pipeline_each(bands, [set_canada_as_country, | |
strip_punctuation_from_name, | |
capitalize_names]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment