Ask HN: how to learn functional programming in Python

9 points by DeusExMachina ↗ HN
I have an odd question, to which I don't know if there is an answer: can I learn functional programming in Python?

First, let me answer to the obvious question that is coming to your mind: why? The part of the answer on why I want to learn functional programming should be clear to almost everyone here, so the real question is: why Python?

I know that there are a lot of better functional languages out there. The reason I chose Python is that for for my next project I decided to use Google App Engine, because I think that it is a very cost/time/resource effective way to implement what I want. And Google App Engine uses Python (or Java...)

So, to kill two birds with one stone, I'd like to learn functional programming directly in Python, so that I would be able to learn it as I go implementing my app. I already understand what functional programming is and how to program in a functional style, but going only with what I know I will surely miss something.

Everything I find for Python is object oriented (and I can understand why), but I really would like not to have to learn it in one language and then switch to Python (but I know this could be a better way).

Is this possible? Do you recommend not doing it this way? Can you recommend books/resources/anything? Everything is really appreciated. Thank you very much.

12 comments

[ 3.2 ms ] story [ 31.9 ms ] thread
Functional programming is a technique, and that technique is best expressed using languages designed for the purpose.

Python has enough elements of functional programming languages that it should be relatively easily done, but it won't be 'the real deal'.

The interesting thing is that this exercise will probably make you a better python programmer, mutable state, side effects and parameter modification are 'bad' traits anyway, and getting functions to always return the same result dependent solely on the input parameters is 'good'.

This may be of some help:

http://norvig.com/python-lisp.html

It's intended for lisp programmers to make it to python, but I think plenty of the stuff in there should apply to your situation.

You can program in a functional style using python to an extent BUT... python in many ways puts up roadblocks that make it a less than fulfilling thing to do.

I would say you could become familiar with functional programming in python but you can't experience the full depth of functional programming, for that given your general goals... I would suggest looking at clojure ( runs on jvm and gae )

If you want functional programming on the GAE, aren't Clojure and Scala more obvious choices? Both were designed with the functional style in mind.
As others have pointed out, the best course for your goals (learning functional programming and using GAE) is probably going to be Clojure (or Scala).

Python has taken some idioms from functional languages, but if you try to really code in the functional paradigm you will but heads with the language. Statements and expressions are very sharply divided. Lexical scoping is broken. Lambdas are one line only.

Guido even suggested removing filter, reduce, lambda, and map from Python 3k[1]. If that isn't a signal that the functional paradigm won't be nurtured, I don't know what is.

[1] http://www.artima.com/weblogs/viewpost.jsp?thread=98196

filter and map can be done with list comprehensions, and they usually come out clearer.
lambda and map will be there. reduce will be moved to functools and filter will be removed.
I was introduced to and learned functional programming through Python, beginning with these series of docs:

http://www.ibm.com/developerworks/library/l-prog.html

http://docs.python.org/dev/howto/functional.html

I would say I program primarily, though not strictly, in a functional style in Python, Lisp, and R. For my applications it's more for the purpose of a modularity rather than concurrency-critical issues, so Python's functional programming limitations aren't noticeable (don't know in your case). There is a 'functional' module available for Python which extends on map, filter, and reduce, and lambda's single-epxression limitation does not necessarily make it functional-hostile.

"The part of the answer on why I want to learn functional programming should be clear to almost everyone here"

It isn't clear to me. Why don't you explain why you want to learn functional programming?

Functional programming and all its pros and cons are a recurring theme here on HN, AFAIK.

I did not want to mean that everybody here uses it or advocates it, but I think almost everybody knows what leads a developer to move to functional programming.