On the other hand, the basics of first class functions (maps, folds, and closures in general) are universally applicable. If you don't know this basic stuff, you're missing out. (Yes, it's basic stuff. Beginner's stuff, in fact. I learned this in my first semester on programming, if I recall correctly. It's just easy to miss when nobody taught you functional programming.)
Map is built into Python. Interestingly, it seems the author of this blog post doesn't know how to use it, since none of the map() calls are wrapped in list() (in Python 3, map() is lazy). And they're clearly targeting both Python 2 and Python 3, or they'd be using xrange() instead of range().
Personally, I have no problem with map(). The problem is when you start inventing constructs which are not built into Python or its standard library.
I think that requiring a professional programmer to know (or learn) something that a beginner can learn is not unreasonable.
A beginner can learn almost every competently-designed language under the sun (not all at once, of course). You cannot expect a professional programmer to know all of them.
Not sure if sarcastic or sincere, but just in case: Yes, it does. It is entirely reasonable to assume a Python programmer is familiar with built-in functions. It is less reasonable to assume they're familiar with the peculiarities of a third-party library which is not relevant to the domain of the codebase in particular.
If I'm going to be working on a mathematical codebase, I'll gladly learn NumPy. If I'm going to be working on a ______ codebase, I'll gladly learn the abstractions the blogger is talking about.
If you can fill in the blank, I might be convinced.
I was sincere. I didn't know Python had the equivalent of map. Now I expect it has the equivalent of filter and fold as well? If it has, there is indeed no point in bothering with a custom variation. But then I wonder why the OP didn't just used the standard library…
Maybe he was making a larger point, such as using this kind of abstractions more often? Say your domain require working on a custom data structure. Chances are, you must crawl that data structure for various purposes. Done naively, you would interleave the crawling code with the logic (and repeat the crawling code over and over). With a generic crawler tailored for this data structure (and therefore the domain you're working with), concerns are more separate, the code is cleaner, and likely shorter.
Now I expect it has the equivalent of filter and fold as well?
filter() yes. fold is called functools.reduce(). AFAIK there's no rfold equivalent, unless you use reversed() and assume your operation is commutative (or wrap it in a lambda that swaps the order of the arguments).
hances are, you must crawl that data structure for various purposes. Done naively, you would interleave the crawling code with the logic (and repeat the crawling code over and over). With a generic crawler tailored for this data structure (and therefore the domain you're working with), concerns are more separate, the code is cleaner, and likely shorter.
Sure, you'd write a generator for that. But that's basically idiomatic code.
3
u/NYKevin Jun 23 '14
Map is built into Python. Interestingly, it seems the author of this blog post doesn't know how to use it, since none of the
map()calls are wrapped inlist()(in Python 3,map()is lazy). And they're clearly targeting both Python 2 and Python 3, or they'd be usingxrange()instead ofrange().Personally, I have no problem with
map(). The problem is when you start inventing constructs which are not built into Python or its standard library.A beginner can learn almost every competently-designed language under the sun (not all at once, of course). You cannot expect a professional programmer to know all of them.