|
|
|
|
|
by billfruit
2246 days ago
|
|
One thing I have felt is that python doesn't embrace the functional way of thinking, even to the extent that JavaScript does. I personally find that once I have been exposed to a modern functional approach like in Clojure, etc, I find python lacking. Not just syntactically, but conceptually. For example, IIRC, many list methods in python modify the list they are working on, instead of returning a new list. |
|
Consider:
getNumbers() .map(x => x×2) .filter(x => x % 6 == 0) .map(x => x^2)
get numbers. double them. filter for divisibility by 6. square them.
versus
[x^2 for x in x×2 for x in getNumbers() if x % 6 == 0]
get the square of the doubles version of getNumbers values, but only if those doubles are dividable by 6. But wait are the doubles dividable by 6 or the squares?
Maybe some people are fine with looking at what operations are being performed on the data before even knowing what the data itself is, but that for me seems incredibly backwards. Plus it also gives rise to order of operations ambiguities. Nobody could mistake the ordering in JS, but I honestly have no clue how that python would evaluate.
(using carats because hn cant format code)
Edit- This came to mind because of a flattening list comprehension I encountered earlier today:
#flatten the lists
flattened_list = [y for x in list_of_lists for y in x]
I've spent quite some time staring at this and I still have no clue what it's actually doing. I've never had that with JS operator chains.