Skip to content

Instantly share code, notes, and snippets.

@maryrosecook
Created December 9, 2014 20:16
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 maryrosecook/7773336405e7854a803a to your computer and use it in GitHub Desktop.
Save maryrosecook/7773336405e7854a803a to your computer and use it in GitHub Desktop.
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