13 ms·
Toolz: A functional standard library for Python
- lysium 6y agoI prefer functional style whenever I can. I noticed that this style slows Python programs down. It seems function calls are rather expensive. Does anybody have a similar experience?
- thisiszilff 6y agoYes, I suspect it is a pitfall of a multi-paradigm language that cannot assume so much about code in order to optimize. Opinionated functional languages (ie clojure, haskell) can have a lot more guarantees about what is going on in order to optimize all those function calls, lexical bindings, etc.
- vmchale 6y agoThere's a bunch of oddities in Python, e.g. recursion limits.
- AlexCoventry 6y agoIt's been a while since I knew those kinds of details, but it certainly been the case in the past. https://stackoverflow.com/questions/22893139/why-is-a-function-method-call-in-python-expensive https://stackoverflow.com/questions/22893139/why-is-a-functi...
- heavyset_go 6y agoPEP 590[1] makes calling callables less expensive. It was finalized for Python 3.9. [1] https://www.python.org/dev/peps/pep-0590/ https://www.python.org/dev/peps/pep-0590/
- lysium 6y agoThat sounds promising, thanks!
- vmchale 6y agoThey have to check a recursion limit, for one.
- Grimm1 6y agoI had a bit of a derp here at first, I was like what, python already has a functional (as in working) standard lib, then I read the docs and was like ohhhhh that functional. I think python made some half measures in regards to functional programming, not a bad thing since python tends to blend the different styles decently but at least for me it would be nice to have a good library to extend the functional side a little more, hopefully this scratches the itch.
- seertaak 6y agoFunctional programming in Python is severely hampererd by Python's lambda keyword, which - simply put - sucks.
- 0x2c8 6y agoCan you elaborate on this? What is wrong with Python's `lambda`?
- kissgyorgy 6y agoIt can only have one line for example.
- Iwan-Zotow 6y agono, not correct
- pansa2 6y agoPython’s `lambda` can only contain a single expression. There’s no good way to add support for full anonymous functions to Python’s grammar. One of the rules that makes significant-whitespace work elegantly is that statements can contain expressions, but never vice-versa.
- omaranto 6y agoPlenty of languages with significant-whitespace have multiline anonymous functions, like Haskell, Standard ML, Ocaml, etc. Maybe it's no possible in Python for a student reason, but the reason is not that the syntax is bad on indentation.
- pansa2 6y agoThe syntaxes of those languages are fundamentally different from Python’s. They don’t have an “expressions may not contain statements” rule - they don’t even have statements.
- vorticalbox 6y agoI've been writing functional code for a year or so now at work with the help of ramda in nodejs, there is also a port for python. My colleagues don't like becuase it's so different to what they are used to but I am putting out code faster and with less bugs so I'm not going to stop.
- deleted 6y ago[deleted]
- zelphirkalt 6y agoWhy use a library for it? Can it not be done with just Python? And if using the library is much different from normal Python, does it mitigate Python's problems with functional programming? (For example one expression only lambdas and no TCO.) I also do use some functional concepts in my Python work, but do not use a library for it. Only procedures or functions. No additional dependencies.
- gugagore 6y agoI think `grouby` is a compelling example: https://toolz.readthedocs.io/en/latest/control.html#other-patterns https://toolz.readthedocs.io/en/latest/control.html#other-pa... > Most programmers have written code exactly like this over and over again, just like they may have repeated the map control pattern. When we identify code as a groupby operation we mentally collapse the detailed manipulation into a single concept. If you don't use a library, then you have to re-write something like groupby many times, I would expect. Or WORSE, you don't even use the pattern, writing "code exactly like this over and over again".
- zelphirkalt 6y agoHuh, interesting. This does not remind me of the database groupby operation, but rather of partition, like in SRFI-1. I mean in natural language it is clear to me, why they'd name it groupby, but in programming terms I think partition is more appropriate, as groupby is already "blocked" by the database operation. Often one only needs one of the partitions though, which is when filter is sufficient. Otherwise I guess one can easily write partition oneself and then use that function over and over again, without resorting to a library. But perhaps it is a good example, so that you do not have to write partition in every project and if the additional dependency is OK to have, why not, if it is indeed a good one.
- jonmoore 6y agoToolz, like seemingly everything Matt Rocklin is a major contributor to, is something of a model library: cleanly designed and coded, with strong documentation. Although Python is not going to match a full Lisp, Haskell or ML in all their strengths, using a functional style can be useful and expressive. The toolz docs give some relevant background at https://toolz.readthedocs.io/en/latest/heritage.html https://toolz.readthedocs.io/en/latest/heritage.html . At a language level, Peter Norvig gave a lengthy comparison of Python and Lisp at https://norvig.com/python-lisp.html https://norvig.com/python-lisp.html in 2000.
- jgalt212 6y agoI've always found logging and debugging to be just so darn tedious in functional realms. e.g. how to do easily you log/instrument f(g(h(x)))?
- rcv 6y agoNot sure how easy convenient this is in practice, but the docs mention using the "do" function [1] [1] https://toolz.readthedocs.io/en/latest/api.html#toolz.functoolz.do https://toolz.readthedocs.io/en/latest/api.html#toolz.functo...
- ElevenPhonons 6y agoPerhaps a custom compose function can help with these use cases? This series has a few examples of composing computation in Python that might be useful. https://github.com/mpkocher/Functional-Programming-Techniques-In-Python https://github.com/mpkocher/Functional-Programming-Technique... And there's a general list of resources here. https://github.com/sfermigier/awesome-functional-python https://github.com/sfermigier/awesome-functional-python
- rcv 6y agoLooks like a very nice more functional-focused alternative to pydash[1] (which is a Python port of lodash.js, which in turn is a superset of another library called underscore.js - whew). [1] https://pydash.readthedocs.io/en/latest/index.html https://pydash.readthedocs.io/en/latest/index.html
- mujina93 6y agoFor a type safe approach to functional programming in python, try returns (https://returns.readthedocs.io/en/latest/ https://returns.readthedocs.io/en/latest/).
- pansa2 6y agoIt always amazes me the lengths people will go to to avoid learning & using a different language. Perhaps if you want statically-typed functional programming, you shouldn’t be using Python? It’s not the best choice for that - in fact, it’s almost the worst choice.