I strongly think Python should have more functional programming support. Functional Programming Languages are usually scary to look at for many programmers. Python would not only be a good FP introduction to them, it would also benefit greatly.<p>Years ago I found out the Guido wouldn't let tail recursion included, and even tried to remove map function from built in functions. Therefore I got the impression that python wouldn't have further support in FP. I really wish that is not the case. With the coming pattern matching in 3.10, My hope is high again.<p>I have very high respect for Guido van Rossum, I'm not here to discredit him. He's one of the authors in PEP 634.<p>I wish python would have simpler syntax for lambda which is currently using the lambda expression, even JS is doing better on this. A built-in syntax for partial would also be great. It could be even better if we can have function composition.<p>Some problems are better solved the FP way. It could even make the program more readable which is one of the strengths of Python.
The worst part of python is the lack of utility functions for munging collections. But it sits at a slightly higher level than this - things that are idiomatic in other scripting languages like groupBy(fn), sortBy(fn), countBy(fn), collate, are all inexplicably like IKEA self assembled furniture in Python instead of first class entities. It makes lots of routine data munging and algorithmic work much more annoying than it needs to be (comparing to Groovy, Ruby, etc).
Site is down. I believe this is the same article by the author.<p><a href="https://towardsdatascience.com/functools-the-power-of-higher-order-functions-in-python-8e6e61c6e4e4" rel="nofollow">https://towardsdatascience.com/functools-the-power-of-higher...</a>
Heh, a younger me would definitely be walking around with a black T-shirt with:<p><pre><code> from functions import *
</code></pre>
…on the front and<p><pre><code> @cached_property
def troll_face(self):
return ‘:)’
</code></pre>
…on the back.<p><i>functools</i> and <i>itertools</i> are amazing and I love them both. They are especially useful for teaching high school CS without having to stray from Python, which the kids at all levels know well and are comfortable with.<p>However.<p>Using <i>@cached_property</i> feels like a bad code smell and it’s a controversial design decision. The example given is more like an instance of a factory? Perhaps if the result of the render function was an Important Object™ (not just a mere string) then the function’s callers might not be so cavalier about discarding the generated instance.<p>I would like to see the calling code that calls `render` more than once in two different places (hence the need for caching with no option for cache invalidation?). When every stack frame is a descendant of main() then there’s no such thing as “two different places”!<p><pre><code> def main():
page = HN().render()
while ‘too true’:
do_work(page)
tea_break(page)
</code></pre>
Ironically it doesn’t feel very functional.
After having done Scala for a few years, it's finally a pleasure to be able to read and understand a thread like this.<p>FP gains are real. Anyone who tells you otherwise doesn't know what FP is.<p>To anyone out there who isn't on the FP train yet, get on. You'll become a better programmer for it.
I am delighted every time the Python Standard Library is mentioned. Always has a nice surprise.<p>The last time I was surprised was the itertools library.
Functional programming in Python sucks for a few reasons:<p>- Lack of pipe operator<p>- Multiple arguments everywhere instead of currying<p>- No do-notation or equivalent<p>- Reference equality rather than structural equality for objects etc.<p>If you want to program in this style, consider using F#, R or even JavaScript with a few Babel plugins.
Big shoutout to lru_cache. I tossed in two lines of code and was able to get a 60x speedup in my code by reducing the amount of regular expression compilations I had to do.
For people wanting to do more functional programming in Python, have a look at toolz (<a href="https://toolz.readthedocs.io/en/latest/" rel="nofollow">https://toolz.readthedocs.io/en/latest/</a>). It goes a bit further in that direction.
The only thing I really think is missing from python is good pattern matching (curious to try out the new implementation), and a pipe operator.<p>Chaining functions is somewhat annoying in python if you want to do any sort of cross library work (which is super common in the data side of the language). Pandas has nice function chaining, but if you want chain data through cleaning functions and maybe do some nlp work you don't end up with a very readable or nice syntax IMO.
Not really for use with python, but still worth a look since it brings some interesting syntax along.<p><a href="http://coconut-lang.org/" rel="nofollow">http://coconut-lang.org/</a><p>> Coconut is a functional programming language that compiles to Python. Since all valid Python is valid Coconut, using Coconut will only extend and enhance what you're already capable of in Python.<p>> Why use Coconut? Coconut is built to be useful. Coconut enhances the repertoire of Python programmers to include the tools of modern functional programming, in such a way that those tools are easy to use and immensely powerful; that is, Coconut does to functional programming what Python did to imperative programming. And Coconut code runs the same on any Python version, making the Python 2/3 split a thing of the past.
Python <i>sucks</i> for functional programming.
Mutability, references and hidden objects everywhere.<p>If you want a real dynamic language functional programming experience, you would be far better with Elixir.
Don’t use lru_cache. It stores pointers to the values, so if a mutable value is changed downstream it gets changed in the cache.<p>That’s not proper caching.
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at <a href="https://martinheinz.dev:8080/api/v1/posts/52" rel="nofollow">https://martinheinz.dev:8080/api/v1/posts/52</a>. (Reason: CORS request did not succeed).
Note to author: I am unable to parse this sentence: "It's a simple wrapper on top of the lru_cache which omits the max_size argument making it smaller and after as it doesn't need to evict old values."