by ilovecaching · 8y ago · 0 comments
I can approach writing Python essentially the same way I’d write Haskell using the above strategy. I think it looks really nice.
I’ve turned a few people away from the dark path of OOP by writing in this style as well. They’re usually turned when they see how easy it is to test just regular functions with no need for magic mocks and the like.
FP is also quite idiomatic. Pythons list comprehensions are quite powerful, and the libraries I mentioned in my previous comment are all part of the standard library. There are third party libraries that attempt to make Python even more functional.
I wouldn't like to maintain Haskell code written by someone who thinks everything should look like Python. And vice-versa.
It’s so hard to even check where the actual implementation is....
There are no comments in code, no documentation whatsoever...
Refential transparency and simple data structures makes code much simpler to understand, and leads to fewer surprises when the code is being modified.
Anyone who learns Python will necessarily learn OOP as it’s the de facto style. The post is a resource for that. My comment is meant to encourage people to consider the alternative ways you can approach problems in Python.
Checkout http://wiki.c2.com/?ClosuresAndObjectsAreEquivalent
> from inspect import type
> type([])
=> <class 'list'>
> type(lambda x : x)
=> <class 'function'>
> type(1)
=> <class 'int'>
> type([x * x for x in [1,2,3]])
=> <class 'list'>
> type((x * x for x in [1,2,3]))
=> <class 'generator'>
> ...>>> def odd(n): return n % 2 == 1
...
>>> [ i if odd(i) else -i for i in range(1, 11) ]
[1, -2, 3, -4, 5, -6, 7, -8, 9, -10]
In [8]: f = lambda x: x * 2
In [9]: f(2)
Out[9]: 4
In [10]: g = lambda x: if x % 2 == 0: x * 2 else x * 3
File "<ipython-input-10-f7248669b8fd>", line 1
g = lambda x: if x % 2 == 0: x * 2 else x * 3
^
SyntaxError: invalid syntaxYou can write python in a functional style, but you're still going to use OOP constructs.