Created
December 9, 2014 20:16
-
-
Save maryrosecook/7773336405e7854a803a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
bands = [{'name': 'sunset rubdown', 'country': 'UK', 'active': False}, | |
{'name': 'women', 'country': 'Germany', 'active': False}, | |
{'name': 'a silver mt. zion', 'country': 'Spain', 'active': True}] | |
def assoc(_d, key, value): | |
from copy import deepcopy | |
d = deepcopy(_d) | |
d[key] = value | |
return d | |
def call(fn, key): | |
def apply_fn(record): | |
return assoc(record, key, fn(record.get(key))) | |
return apply_fn | |
def pipeline_each(data, fns): | |
return reduce(lambda a, fn: map(fn, a), | |
fns, | |
data) | |
print pipeline_each(bands, [call(lambda x: 'Canada', 'country'), | |
call(lambda x: x.replace('.', ''), 'name'), | |
call(str.title, 'name'), | |
pluck(['name', 'country'])]) | |
# Implement pluck so that the pipeline_each call | |
# above returns the bands below. | |
# => [{'name': 'Sunset Rubdown', 'country': 'Canada'}, | |
# {'name': 'Women', 'country': 'Canada' }, | |
# {'name': 'A Silver Mt Zion', 'country': 'Canada'}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment