Hacker News new | ask | show | jobs
by wonderzombie 5072 days ago
map and filter feel quite natural to me, and superior. I like FP but I don't consider myself an "FP person," if only because I don't spend most of my time in it, nor am I particularly well versed in any FP language.

Python occupies an interesting middle ground which might help you see what I mean. I vastly prefer

   filtered_xs = [x for x in xs if x.foo(bar)]
to

   filtered_xs = []
   for x in xs:
     if x.foo(bar):
       filtered_xs.append(x)
If you think of filtering and mapping as trivial operations, it seems like a waste to spend so many words on it as in the latter case. It feels tedious, like filler, since I have to explain how to iterate over a list every time I write a loop. When you can express it crisply and just as precisely in a single line as in the former, the reader is free to occupy their mind with the real work. When you can deal with a higher level of abstraction without introducing a lot of complexity, verbosity, or confusion, I think that's a win.

FTR, the two mainstream languages that frequently emulate this include Ruby (see Array) and, depending on what you're using, JavaScript. If you've played with jQuery, f'rex, you can see this pattern there. (And even monad-like behavior!)