8 ms·
Toolz: A functional standard library for Python
- jboynyc 11y agoBetween this an fn.py (https://github.com/kachayev/fn.py https://github.com/kachayev/fn.py), functional programming in Python has really good library support beyond the standard library.
- kmike84 11y agohttps://github.com/Suor/funcy https://github.com/Suor/funcy is also nice
- deleted 11y ago[deleted]
- boothead 11y agoI love libraries like this and fn.py, and I really miss the things I've got used to having available in Haskell when I write python. It's possible to use function patterns in python (Applicative and Functor are things you can implement in any language). However given who is in charge of python and the way he feels about FP, I don't think python will ever be a decent functional programming environment: As long as I'm "in charge" the chances of this (or anything like it) being accepted into Python are zero. I get a headache when I try to understand code that uses function composition, and I end up having to laboriously rewrite it using more traditional call notation before I move on to understanding what it actually does. Python is not Haskell, and perhaps more importantly, Python users are not like Haskell users. Either way, what may work out beautifully in Haskell will be like a fish out of water in Python. I understand that it's fun to try to sole this puzzle, but evolving Python is more than solving puzzles. Enjoy debating the puzzle, but in the end Python will survive without the solution. http://stackoverflow.com/questions/1017621/why-isnt-python-very-good-for-functional-programming http://stackoverflow.com/questions/1017621/why-isnt-python-v...
- scardine 11y agoFunction call is somewhat expensive in Python compared to most languages, so I guess there are reasons other than personal taste for why functional composition is not blessed.
- imakesnowflakes 11y agoI just hope he (and his successors) maintain the same uncompromisingly critical approach when it comes to adding features to the language.. I think the lack of some things like this, is a sort of reminder to the programmer that says, "hey you, you are writing Python. Not Haskell. Do not go that way because we don't have stuff to deal with what you may find further down that road". Nothing ruins a language like adding a bunch of poorly thought out half baked features, that does not fit well with the rest of it...
- boothead 11y agoAnd yet a language has to evolve or become obsolete...
- toyg 11y agoI would argue that the really necessary evolution is often in implementation facilities, rather than core language constructs. C++ has evolved more than most, but people who don't choose C++ do it mostly because of other considerations. You could have the most functional C++ version ever, and still people would not use it for simple web apps.
- ubernostrum 11y agoWhich is not the same as "a language must evolve into my preferred design or become obsolete". Python has made it through years of criticism from people who misunderstand Guido's stance on FP in Python (and before that from people who disliked his stance on tail call optimization) without being obsoleted by pure FP languages.
- afarrell 11y agoWhich, like apoptosis, is eventually a good thing.
- wodenokoto 11y agoThat is not what stemming is at all. Stemming is going from "running" to "run", not "run!" to "run" http://en.m.wikipedia.org/wiki/Stemming http://en.m.wikipedia.org/wiki/Stemming
- swasheck 11y agoagreed. i'm not sure why one would choose to implement an inadequate function here over against something like nltk (http://www.nltk.org/book/ http://www.nltk.org/book/), which is excellent.
- deleted 11y ago[deleted]
- kraymer 11y agoTangential but ... could someone give me the name of the python library that made the front page last month and that "extends the standard lib" (itertools recipes, dates helper functions, etc)? Can't find it using HN search. Email in my profile. thks
- estefan 11y agohttps://github.com/mahmoud/boltons https://github.com/mahmoud/boltons https://news.ycombinator.com/item?id=9350562 https://news.ycombinator.com/item?id=9350562
- agumonkey 11y agoYear old reddit thread http://www.reddit.com/r/Python/comments/1wrlji/ http://www.reddit.com/r/Python/comments/1wrlji/
- kmike84 11y agoIt is sometimes hard to justify a dependency - many of toolz/fn.py/funcy functions are not hard to write manually. The great thing about toolz is its https://github.com/pytoolz/cytoolz https://github.com/pytoolz/cytoolz counterpart which provides an optimized implementation of toolz. It is useful when you have a bottleneck in Python code - e.g. run several functions (functoolz.juxt), merge dictionaries (dicttooz.merge), etc. With cytoolz the code becomes prettier and faster, not only prettier, so it is easier for cytoolz to sneak in.
- mrocklin 11y agoInteresting. What are your thoughts on the costs of dependencies? What about toolz makes you hesitate to depend on it?
- pionar 11y agoIt seems to me that if you want functional programming, you choose a functional language. You don't (excuse my language) bastardize an OO language for it.
- rnhmjoj 11y agohttps://gist.github.com/rnhmjoj/5ce4a571a624bd42c771 https://gist.github.com/rnhmjoj/5ce4a571a624bd42c771 I created a bastard.
- alvare 11y agoI always include toolz in my Python projects. It's also useful as a compatibility wrapper.
- nilkn 11y agoI love stuff like this, though I feel it makes the code actually harder to read because none of it's standardized and there are a variety of somewhat equivalent but not completely isomorphic libraries out there for functional programming in Python.
- calebm 11y agoDoes it have a recursive dictionary merge - something like merge({'a': {'b': 1}}, {'a': {'c': 2}}) -> {'a': {'b': 1, 'c': 2}} ?
- mrocklin 11y agoYou can manage this by composing merge and merge_with In [1]: data = [{'a': {'b': 1}}, {'a': {'c': 2}}] In [2]: from toolz import merge, merge_with In [3]: merge_with(merge, data) Out[3]: {'a': {'b': 1, 'c': 2}} You'd have to be a bit clever to go fully recursive
- alien_robot 11y agoWish I had known I could compose something from existing functional libs, but I created this simple function to solve that very problem. https://gist.github.com/angstwad/bf22d1822c38a92ec0a9 https://gist.github.com/angstwad/bf22d1822c38a92ec0a9
- mrocklin 11y agohttp://toolz.readthedocs.org/en/latest/ http://toolz.readthedocs.org/en/latest/