8 ms·
Functional programming in Python
- deleted 14y ago[deleted]
- Porter_423 14y agoIts totally informative.I like your style dear.
- pepijndevos 14y agoWhat is really missing is a set of decent data structures.
- jlarocco 14y agoWhat do you mean? What would you add on top of the built-ins and stdlib data structures?
- swannodette 14y agoI believe he is talking about persistent data structures.
- roryokane 14y agoLink for convenience: http://en.wikipedia.org/wiki/Persistent_data_structure http://en.wikipedia.org/wiki/Persistent_data_structure “… a persistent data structure is a data structure that always preserves the previous version of itself when it is modified. Such data structures are effectively immutable, as their operations do not (visibly) update the structure in-place, but instead always yield a new updated structure.”
- andreasvc 14y agoThere are situations where balanced search trees are appropriate. A general purpose priority queue (with delete and decrease-key operations); there's actually code for this in the _documentation_ of the heapq module; this seems really odd to me, why not just include it? It's also a shame that heapq is built on list instead of being a first-class data-structure, it feels bolted-on. Bitwise tries would be nice as well.
- sureshv 14y agoEven if heaps were a first class data-structures wouldn't the choice between trees and lists exist in the underlying implementation? Ultimately you would have to allow for both or deal with the strengths and weaknesses of that implementation.
- andreasvc 14y agoAs far as I know array-based heaps are most efficient with practical workloads (because it doesn't have the overhead & non-locality due to pointers for a tree structure). I don't think it's necessary to offer multiple implementations, just as there is only a single well tuned hash table in Python. What matters is that the functionality of an abstract data type is offered.
- chimeracoder 14y agoI doubt you'll see that - at least in CPython. Guido's been very clear about how he feels about implementing functional paradigms in Python, so it will probably never support functional programming in the standard implementation - just a bit in syntax/interface.
- soc88 14y agoI honestly don't understand that. Data structures are just code ... isn't it much more work to change syntax and interfaces instead of adding some decent implementations of data structures to the library?
- chimeracoder 14y agoThe syntax changes happened very much against Guido's will - he actively tried to remove lambda (and I believe map as well) for quite a while. Anyone can write whatever Python libraries they want, but I doubt anything of the sort would be added to the standard library, and certainly nothing that would require an FP-aware interpreter to optimize in a useful way.
- soc88 14y agoI agree completely. It is surprising how many Python developers are unfamiliar with it.
- theorique 14y agoThis is a useful reference but not really all that current. It dates back to 2006 - the functional constructs in Python now have evolved and changed over the years.
- chimeracoder 14y agoOne thing that's been key for me is namedtuple (in the collections module). It's immutable like a tuple, but the values can be accessed by name just as if they were object attributes built with the class keyword. It's great for creating generic functions (think Lisp and CLOS) instead of using Python's prototypical system. And since tuples can contain any objects and functions are objects, you can bind callables like lambdas to those names, which allows you to have 'methods' if you want as well. That said, the standard library leaves something to be desired for the functional style. It makes sense for object-oriented programming to have non-mutating functions return the result and mutating functions return None (think sorted vs. sort), but this can get rather irritating when you're trying to write in a purely functional paradigm. Also, am I forgetting my Python, or does it not have a great solution for appending to the front of a list? One thing I love about Lisp is that it's ridiculously easy to write (cons item some-list), or even (cons item1 (cons item2 some-list)). Doing the same thing in Python is irritating, because insert() doesn't return the result list. ...or maybe that's what I deserve for trying to write Lisp-like code in Python, anyway....! EDIT: As noted in the comments, the '+' operator will suffice here. Though since lists are really arrays and not linked lists, this functional way of thinking will result in horribly inefficient CPython code.
- dustingetz 14y agoas has been pointed out, python isn't particularly great at doing hardcore functional programming due to lack of native persistant datastructures. however i've found it great for learning functional programming without having to get used to the syntax of real functional languages. here are some different ways to implement functional sequence operations without native python syntax like `yield`: https://github.com/dustingetz/sandbox/blob/master/etc/lazy.py https://github.com/dustingetz/sandbox/blob/master/etc/lazy.p... https://github.com/dustingetz/sandbox/blob/master/etc/map.py https://github.com/dustingetz/sandbox/blob/master/etc/map.py and an unfinished, sloppy impl of monads in python with lots of minor errors: http://www.dustingetz.com/2012/04/07/dustins-awesome-monad-tutorial-for-humans-in-python.html http://www.dustingetz.com/2012/04/07/dustins-awesome-monad-t...
- andreasvc 14y agoWhat do you mean with lack of persistent data-structures? There are immutable data-structures such as numbers, strings, tuples, and frozensets (unfortunately no frozen dicts) -- seems plenty to me. Or do you mean the technical meaning of persistent data-structures, as in one that gives access to all previous versions?
- swannodette 14y agoA good set of persistent data structures would give you all the generality, utility, and performance of Python data structures like lists and dicts and none of the mutability.
- recursive 14y agoI don't know about dicts, but as far as I can tell, tuples give you all the generality, utility, and performance of lists.
- swannodette 14y agoPerformance? Unless I'm mistaken you can't update tuples efficiently.