Skip to content

Instantly share code, notes, and snippets.

@rileyjshaw
Created March 26, 2014 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rileyjshaw/9793744 to your computer and use it in GitHub Desktop.
Save rileyjshaw/9793744 to your computer and use it in GitHub Desktop.
Two pipeline functions: map(reduce(stuff)) and reduce(map(stuff))
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