Live data from Hacker News

Functional programming in Python

docs.python.org

31–40 of 64 posts

Re: Functional programming in Python

#31

Earlier quoted context omitted.

What 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?

A 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.

I don't know about dicts, but as far as I can tell, tuples give you all the generality, utility, and performance of lists.

Re: Functional programming in Python

#32

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/dus…

I would have thought the broken statement/expression dichotomy would be a bigger annoyance, though I've never used Python in anger myself.

Re: Functional programming in Python

#33

Earlier quoted context omitted.

A 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.

I don't know about dicts, but as far as I can tell, tuples give you all the generality, utility, and performance of lists.

Performance? Unless I'm mistaken you can't update tuples efficiently.

Re: Functional programming in Python

#34

Earlier quoted context omitted.

I don't know about dicts, but as far as I can tell, tuples give you all the generality, utility, and performance of lists.

Performance? Unless I'm mistaken you can't update tuples efficiently.

How do you intend to "update" an immutable list? Isn't it... immutable?

Re: Functional programming in Python

#35
post #22
post #12

Earlier quoted context omitted.

That works but it's kind of a pain that the natural pythonic syntax: ['a'].extend(some-list) doesn't return a list.

that is not the natural pythonic syntax. for concatenating lists, "+" is standard

Thanks - looks like plus operator concat was in there in the beginning as well:

http://docs.python.org/release/1.4/lib/node10.html#SECTION00...

Re: Functional programming in Python

#36
post #34

Earlier quoted context omitted.

Performance? Unless I'm mistaken you can't update tuples efficiently.

How do you intend to "update" an immutable list? Isn't it... immutable?

As a pure function where the returned value utilizes structural sharing with the input values.

Re: Functional programming in Python

#37
post #5

Earlier quoted context omitted.

What do you mean? What would you add on top of the built-ins and stdlib data structures?

There 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.

Even 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.

Re: Functional programming in Python

#38
post #34

Earlier quoted context omitted.

Performance? Unless I'm mistaken you can't update tuples efficiently.

How do you intend to "update" an immutable list? Isn't it... immutable?

http://en.wikipedia.org/wiki/Persistent_data_structure

Re: Functional programming in Python

#39

One 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…

namedtuple is awesome, until you realise it works internally by using eval and generating the source code of a class.

Why does it do that, instead of some other more pythonic way?

Re: Functional programming in Python

#40

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/dus…

I would have thought the broken statement/expression dichotomy would be a bigger annoyance, though I've never used Python in anger myself.

I agree so much. Real functional programming requires an expression oriented language.
Post reply on HN