Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Quite often, chains of map/filter/reduce/whatever are more readable because you can see the flow of data, like you were looking at a factory production line. List comprehensions and traditional prefix functions (e.g. map(iterable, function)) completely break the visual chain that makes basic functional code so readable.

Like, which of these make more sense?

strList.filter(isNumeric).map(parseInt).filter(x => x != 0)

[ x for x in [ parseInt(s) for s in strList if isNumeric(s) ] if x != 0]

filter(map(filter(strList, isNumeric), parseInt), lambda x: x != 0)

And it's not like Python doesn't have the language features to implement the first pattern. Map,reduce,filter,etc. could simply be added to the iterable base class and be automatically usable for all lists, generators and more.



> Map,reduce,filter,etc. could simply be added to the iterable base class

Surprisingly, Python doesn't have an iterable base class! `list.__bases__` is just `object`.


I believe list comprehensions would be much more readable if they were written the other way round:

    [for x in [for s in strList: if isNumeric(s): parseInt(s)]: if x != 0: x]
Nesting them is still ugly, but can often be avoided using an assignment expression:

    [for s in strList: if isNumeric(s): if (x := parseInt(s)) != 0: x]




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: