Live data from Hacker News

Functional Programming in Python [pdf]

oreilly.com

11–20 of 62 posts

Re: Functional Programming in Python [pdf]

#11

Hard to tell from a quick scan, but it appears to be slightly more informative than the classic document in the python docs: https://docs.python.org/dev/howto/functional.html

Just in case, a few years back I've seen an article on functional programming in python. Mostly arithmetic but the patterns were very pretty (think Euclid algorithm generalized). I never managed to find it again. If that rings a bell to someone, I'll be forever virtually indebted. - http://www.ibm.com/developerworks/library/l-prog/ - http://kachayev.github.io/talks/uapycon2012/#/ - http://anandology.com/python-practi…

Don't think this is the one you're thinking of, but I do like Mary Rose Cook's intro to fp using Python:

http://maryrosecook.com/blog/post/a-practical-introduction-t...

Re: Functional Programming in Python [pdf]

#12

Yeah. Python has the functional programming features I expect of any modern language. However, I feel that Python has a lot of unneeded syntax. I always prefer apply() over * and map() and filter() over list comprehensions. func(*args) apply(func, args) [func(a) for a in collection] map(func, collection) [a for a in collection if func(a)] filter(func, collection) I don't see why people use all of this special syntax.

It's easy to read, though that is pretty subjective.

Re: Functional Programming in Python [pdf]

#13
post #2

https://en.wikipedia.org/wiki/Python_Bridge "Python Bridge, officially known as High Bridge, is a bridge that spans the canal between Sporenburg and Borneo Island in Eastern Docklands, Amsterdam. It was built in 2001 and won the International Footbridge Award in 2002. The bright red bridge spans 90 meters and was designed by Adriaan Geuze of the architectural firm West 8" Coincidentally, Amsterdam can be considered t…

This reads like Lisp.

Re: Functional Programming in Python [pdf]

#14

Yeah. Python has the functional programming features I expect of any modern language. However, I feel that Python has a lot of unneeded syntax. I always prefer apply() over * and map() and filter() over list comprehensions. func(*args) apply(func, args) [func(a) for a in collection] map(func, collection) [a for a in collection if func(a)] filter(func, collection) I don't see why people use all of this special syntax.

In Python3, you are really looking at...

    (func(a) for a in collection)
    map(func, collection)
as equivalent. If you want a list (and not a generator), you would need to do this:

    [func(a) for a in collection]
    list(map(func, collection))
For me, the first set (comprehensions) of notation has a more mathematical feel to it, i.e. { x^2 | x \in 0...10 }. Just replace the bar with "for" and it's almost the same thing.

I believe the documentation for `filter` even mentions that it is equivalent to the comprehension[1].

[1] https://docs.python.org/3/library/functions.html#filter

Re: Functional Programming in Python [pdf]

#15

Earlier quoted context omitted.

Just in case, a few years back I've seen an article on functional programming in python. Mostly arithmetic but the patterns were very pretty (think Euclid algorithm generalized). I never managed to find it again. If that rings a bell to someone, I'll be forever virtually indebted. - http://www.ibm.com/developerworks/library/l-prog/ - http://kachayev.github.io/talks/uapycon2012/#/ - http://anandology.com/python-practi…

Don't think this is the one you're thinking of, but I do like Mary Rose Cook's intro to fp using Python: http://maryrosecook.com/blog/post/a-practical-introduction-t...

Indeed, this is quite newer, and a gradual introduction to FP idioms (with good successes).

Mine wasn't introductory and just threw out ways to decompose the problem into counter intuitive (think ~monad) blocks.

I'll edit my post to list those I've found so far. Thanks a lot anyway.

Re: Functional Programming in Python [pdf]

#17

Even knowing a lot about FP, I still found this worth skimming for the esoteric Python syntax. When it comes to constructing dictionaries with list comprehensions, I would always do something like this: dict([n, 2 ** n] for n in range(5)) But they pointed out an actual "dict comprehension" that I didn't even realize existed: { n: n ** 2 for n in range(5) } And there is a similar "set comprehension": { n ** 2 for n in…

The #1 and #2 expressions yields different results.

>>> dict([n, 2 n] for n in range(5)) {0: 1, 1: 2, 2: 4, 3: 8, 4: 16} >>> { n: n 2 for n in range(5) } {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}

Re: Functional Programming in Python [pdf]

#20
post #10

Funny how things turned out. I still remember this post[1], it was profoundly disappointing to see Guido's way of thinking. Much of the damage was reversed but it still left an indelible impression that there's a lack of vision for what's going to be important if the language is to stay relevant in the future. [1] http://www.artima.com/weblogs/viewpost.jsp?thread=98196

I actually find his arguments rather convincing, even though I'm partial to a functional approach to programming, myself.
Post reply on HN