Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from functools import *
- 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): #this one does not do anything
- def apply_fn(record): #this is generated function
- return assoc(record, key, fn(record.get(key))) #record=band
- return apply_fn
- def pluck(keys):
- def pluck_fn(record):
- return reduce(lambda elements, key: assoc(elements, key, record[key]), keys, {})
- return pluck_fn
- def pipeline_each(data, functions):
- return reduce(lambda elements, fn: map(fn, elements), #this _elements_ are the data after transformation
- functions,
- data)
- set_canada_as_country = call(lambda x: 'Canada', 'country')
- strip_punctuation_from_name = call(lambda x: x.replace('.', ''), 'name')
- capitalize_names = call(str.title, 'name')
- extract_names_and_countries = pluck(['name', 'country'])
- new_bands = pipeline_each(bands, [set_canada_as_country,
- strip_punctuation_from_name,
- capitalize_names,
- extract_names_and_countries])
- for band in new_bands:
- print(band)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement