News for hackers. Stuff that matters.
Re:Functional Programming in Python [pdf] by delluminatus on Friday July 24, 2015 @04:51PM (#9945100)
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] by rectangletangle on Friday July 24, 2015 @05:07PM (#9945200)
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] by delluminatus on Friday July 24, 2015 @05:11PM (#9945219)
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] by aaronchall on Friday July 24, 2015 @09:57PM (#9946253)
Yes, in Python 2.7 onward:

  s = {i**2 for i in range(10) if i} 
and the colon is what makes the dict comprehension different from the set comprehension:

  d - {i:i**2 for i in range(10) if i}
Re:Functional Programming in Python [pdf] by aaronchall on Friday July 24, 2015 @09:54PM (#9946250)
Although Raymond Hettinger has also called them generator "comprehensions" in the early proposals, the current documentation calls them "generator expressions". And good examples.

Here's how you make those set/dict whatever comprehensions in Python 2.6, before the native syntax is used:

  s = set(i for i in xrange(10) if not i%2)
Re:Functional Programming in Python [pdf] by rectangletangle on Friday July 24, 2015 @10:00PM (#9946256)
Seems like a comprehension is just a particular form of Python expression.
Re:Functional Programming in Python [pdf] by aaronchall on Saturday July 25, 2015 @01:26AM (#9946659)
Here's the grammar file: https://docs.python.org/2/reference/grammar.html

Look for dictorsetmaker (for {}) testlist_comp (for generator expressions) and listmaker (for [], i.e. list comprehensions).

Re:Functional Programming in Python [pdf] by sibsibsib on Friday July 24, 2015 @04:58PM (#9945145)
it was added in python 2.7 / 3.0
Re:Functional Programming in Python [pdf] by devy on Friday July 24, 2015 @06:04PM (#9945484)
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}