Live data from Hacker News

Functional Programming in Python [pdf]

oreilly.com

1–10 of 62 posts

Re: Functional Programming in Python [pdf]

#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 the birthplace of Python, where Guido used to work at the Center for Mathematics and Computer Science (CWI).

And now for some obligatory functional python. Run with

    python lambda.py 2>&1 | head -c 200
to avoid filling your screen with exhausted recursion depth. Notice any pattern in the output?

    import sys
    def c(j,t):
     sys.stdout.write(j('.')('P'))
     return t
    (lambda z:lambda y:z(z(y(lambda p:lambda n:(lambda s:lambda z:z(lambda x:
    lambda y:y)(lambda d:p(s)(y(s))(d)))(lambda x:lambda a:lambda s:lambda p:
    p(a)(lambda y:s(n(x))(y))))(lambda c:lambda a:lambda s:z(lambda y:s(c)(y)
    ))))(y(lambda p:lambda b:lambda t:t(c(b,p)))))(lambda s:lambda p:p(lambda
    x:lambda y:x)(s))(lambda f:(lambda q:q(q))(lambda x:f(lambda y:x(x)(y))))

Re: Functional Programming in Python [pdf]

#3
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 range(5) }
Always amazes me how you can use Python for so many years and still encounter new features in the language.

Re: Functional Programming in Python [pdf]

#5

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…

it was added in python 2.7 / 3.0

Re: Functional Programming in Python [pdf]

#6

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…

This is part of the generalized comprehension syntax. You can also do lazy generator comprehensions.

    a = (i for i in range(10) if i % 2 == 0)
    print(list(a))
You can omit the parenthesis, and use them in calls which expect an iterable.

    b = max(i for i in range(0, 10) if i % 2 == 0)
    print(b)

Re: Functional Programming in Python [pdf]

#7

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…

This is part of the generalized comprehension syntax. You can also do lazy generator comprehensions. a = (i for i in range(10) if i % 2 == 0) print(list(a)) You can omit the parenthesis, and use them in calls which expect an iterable. b = max(i for i in range(0, 10) if i % 2 == 0) print(b)

It's not exactly the same, which is what I thought was interesting. My first example did use a generator expression inside the dict() constructor, but in that case you need to specify the key and value in a tuple or a list.

With the dictionary comprehension you can just separate the key and value with a colon, which is more natural. It might just be sugar on top of a generator expression but it is definitely a special case, syntactically speaking.

Re: Functional Programming in Python [pdf]

#8
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.

Re: Functional Programming in Python [pdf]

#9

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-practice-book/functional-progra...

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

- http://rosettacode.org/wiki/Numerical_integration#Python

Re: Functional Programming in Python [pdf]

#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

Post reply on HN