17 ms·
as has been pointed out, python isn't particularly great at doing hardcore functional programming due to lack of native persistant datastructures. however i've
by dustingetz 14y ago
as 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.
- nknight 14y agoHow do you intend to "update" an immutable list? Isn't it... immutable?
- snprbob86 14y agoAs a pure function where the returned value utilizes structural sharing with the input values.
- swannodette 14y agohttp://en.wikipedia.org/wiki/Persistent_data_structure http://en.wikipedia.org/wiki/Persistent_data_structure
- lihaoyi 14y agoImmutable lists can be preppended/popped in O(1)time, without affecting previous versions. Immutable dicts can have stuff added and removed in O(log n) time, without affecting previous versions. Scala's immutable vectors can have appending/prepending/concatenation/splitting/insertion/deletion in O(log n) time, without affecting previous versions. it's log base 32, so for any arrays using integer indexes, it'll never go above ~7, so it's basically constant. Basically, you get (almost) the performance of mutable data structures, except you don't mess up the old versions of the thing every time you change something. It's really pretty incredible, the performance characteristics people manage to get with structural sharing: http://www.scala-lang.org/docu/files/collections-api/collections_40.html http://www.scala-lang.org/docu/files/collections-api/collect...
- soc88 14y agoThe Scala stuff is impressive. Updated docs at the new site: http://docs.scala-lang.org/overviews/collections/performance-characteristics.html http://docs.scala-lang.org/overviews/collections/performance... The new ConcurrentTrie is also a great addition to 2.10.
- pjscott 14y agoA tuple, behind the scenes, is just a type tag, a refcount, a length, and an array of PyObject pointers. A list, instead of having a fixed array of PyObject pointers, has a pointer to a dynamically resizable array of them. Tuples and lists are both iterable, they both have O(1) indexing and fairly efficient internal representations, but the key difference is mutability. A tuple is supposed to be a composite data type; a list is a dynamic array that you store things in. Aside from mutability, though, tuples can do pretty much anything lists can, and usually a bit more efficiently.
- technomancy 14y agoI would have thought the broken statement/expression dichotomy would be a bigger annoyance, though I've never used Python in anger myself.
- Rickasaurus 14y agoI agree so much. Real functional programming requires an expression oriented language.
- SiVal 14y agoI was at a Python user group meeting with Guido a few years back, and I asked him about his plans for functional programming support in Python. He responded that he thought functional programming was a lot less useful than all the hype would suggest, and that, far from planning more support for functional programming, he regretted having put in as much support as he already had. Ideally, he said, he would remove things like lambda instead of making them stronger. Even things like tail call elimination, he said, would just encourage people to use more recursion, when iteration was easier to read, and in places where TCE would work, the programmer could obviously convert it to iteration himself and should do so for code clarity. I like Python, and I like a lot of things about functional programming, so it wasn't the answer I was hoping for. Unless Guido has had a change of heart since then, I wouldn't pin my hopes on Python ever becoming a first-rate functional programming language. It's first rate at the things Guido values.
- gbog 14y agoI don't know. It seems to me, from reading SICP, that functional programming doesn't mean lambdas or map reduce, it means pure functions as the main programming unit, instead of object or instruction or declaration. Then it is possible to write functional code in python without lambdas, map, using streams (iterators) as the state keeping data structure.
- ubernostrum 14y agoThe problem, as always in these discussions, is that no-one wants to define the term "functional programming". Many people will be quite willing, though, to jump on each other for saying things that they believe flow from an incorrect definition of the term. Anyway, at the very least there's two camps. On the one side, you have the hardcore Scheme folks whose conception of functional programming is "if I can't figure out how to write it with tail recursion it must not be computable". On the other side you have the Haskell folks whose conception of functional programming is "a monad is just a monoid in the category of endofunctors, what's the problem?" Somewhere in between are people who actually get things done.
- 14y ago
- craigyk 14y agoI don't remember where I read it, but "watching someone try to do functional programming in python can be painful". I have to agree quite a bit with that. The distinction between lambdas and functions, expressions, etc. I started doing a lot of programming in CoffeeScript a few months ago, and between the JS underpinnings and the CS syntax you can do things that would just be painful in Python.