Reading Mary Rose Cook's Practical Introduction to Functional Programming, she give as an example of an anti-pattern
def format_bands(bands): for band in bands: band['country'] = 'Canada' band['name'] = band['name'].replace('.', '') band['name'] = band['name'].title()
- the function does more than one thing
- the name isn't descriptive
- it has side effects
As a proposed solution, she suggests pipelining anonymous functions
pipeline_each(bands, [call(lambda x: 'Canada', 'country'), call(lambda x: x.replace('.', ''), 'name'), call(str.title, 'name')])
However this seems to me to have the downside of being even less testable; at least format_bands could have a unit test to check if it does what it's meant to, but how to test the pipeline? Or is the idea that the anonymous functions are so self-explanatory that they don't need to be tested?
My real-world application for this is in trying to make my
pandas code more functional. I'll often have some sort of pipeline inside a "munging" function"
def munge_data(df) df['name'] = df['name'].str.lower() df = df.drop_duplicates() return df
Or rewriting in the pipeline style:
def munge_data(df) munged = (df.assign(lambda x: x['name'].str.lower() .drop_duplicates()) return munged
Any suggestions for best practices in this kind of situation?