This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author dwt
Recipients dwt
Date 2012-01-17.09:48:36
SpamBayes Score 1.6830054e-10
Marked as misclassified No
Message-id <1326793717.4.0.393580852991.issue13804@psf.upfronthosting.co.za>
In-reply-to
Content
Code that uses higher order methods is often the clearest description of what you want to do. However since the higher order methods in python (filter, map, reduce) are free functions and aren't available on collection classes as methods, using them creates really hard to read code.

For example, if I want to get an attribute out of an array of objects and concatenate the results it's two steps, get the attributes out of the object, then concatenate the results.

Without higher order methods its like this. Uses three lines, intermediate variable is quite clear, but hard to compose and hard to abstract out parts of it.

 self.questions = []
 for topic in self.topics:
   self.questions.append(topic.questions)

if I want to do it with higher order functions there's really two ways, do it in one step with reduce:
 self.questions = reduce(lambda memo, topic: memo + topic.questions, self.topics, [])

Or use two steps with the operator module
 self.questions = reduce(operator.add, map(operator.attrgetter('questions'), self.topics), [])

Of these thee first still couples two steps into one lambda, while the second one decoples everything nicely but is a total train wreck to read, as you need to constantly jump back and forth in the line to understand what it does.

Having map and reduce defined on collections would not only make it drastically shorter but also allows you to read it from front to back in one go. (Ok, there is still the caveat that the assignment is in the front instead of at the end, but hey)

 self.questions = self.topics.map(attrgetter('questions')).reduce(add, [])

That would be nicely separated into individual steps that exactly describe what each step is actually doing, is easy to abstract over and actually very succinct.
History
Date User Action Args
2012-01-17 09:48:37dwtsetrecipients: + dwt
2012-01-17 09:48:37dwtsetmessageid: <1326793717.4.0.393580852991.issue13804@psf.upfronthosting.co.za>
2012-01-17 09:48:36dwtlinkissue13804 messages
2012-01-17 09:48:36dwtcreate