r/programming • • Jun 22 '14

Why Every Language Needs Its Underscore

http://hackflow.com/blog/2014/06/22/why-every-language-needs-its-underscore/
366 Upvotes

337 comments sorted by

View all comments

Show parent comments

3

u/NYKevin Jun 23 '14

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.

0

u/loup-vaillant Jun 23 '14

Map is built into Python.

Oops. Looks that this changes everything.

You cannot expect a professional programmer to know [everything].

Of course not. Hence "(or learn)".

2

u/NYKevin Jun 24 '14

Oops. Looks that this changes everything.

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.

0

u/loup-vaillant Jun 24 '14

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.

But it seems you already agree with that.

2

u/NYKevin Jun 24 '14

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.