Proposal: Python's list, generator, and dictionary comprehensions


(Amir Michail) #1

Python examples:

l = [x*x for x in range(10)] // list comprehension

l2 = [(x,y) for x in range(10) for y in range(10) if x + y < 8] // another list comprehension

g = (x*x for x in range(10)) // generator comprehension

d = {x:x*x for x in range(10)} // dictionary comprehension

I think most programmers would like using these. They are concise and easy to understand.


(Paul Ossenbruggen) #2

I like this in python. +1

···

On Dec 17, 2015, at 8:26 AM, Amir Michail via swift-evolution <swift-evolution@swift.org> wrote:

Python examples:

l = [x*x for x in range(10)] // list comprehension

l2 = [(x,y) for x in range(10) for y in range(10) if x + y < 8] // another list comprehension

g = (x*x for x in range(10)) // generator comprehension

d = {x:x*x for x in range(10)} // dictionary comprehension

I think most programmers would like using these. They are concise and easy to understand.

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Liam Butler-Lawrence) #3

Hi Amir,

Comprehensions are great in Python. However, Swift can already do all of these things via for...in, map() and/or generate(). Your examples:

Ex. #1

let l = 1...10.map { (Int) -> (Int) in
    return x*x
}

Ex. #2

let l2: [(Int, Int)]
for x in 1...10 {

    for y in 1...10 where (x + y) < 8 {
        l2.append((x, y))
    }
}

Ex. #3

let g = (1...10.map { (Int) -> (Int) in
    return x*x
}).generate()

Ex. #4

let d = [Int: Int]()

for x in 1...10 {
    d[x] = x*x
}

Liam

···

Sent from my iPhone

On Dec 17, 2015, at 11:26 AM, Amir Michail via swift-evolution <swift-evolution@swift.org> wrote:

Python examples:

l = [x*x for x in range(10)] // list comprehension

l2 = [(x,y) for x in range(10) for y in range(10) if x + y < 8] // another list comprehension

g = (x*x for x in range(10)) // generator comprehension

d = {x:x*x for x in range(10)} // dictionary comprehension

I think most programmers would like using these. They are concise and easy to understand.

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Adrian Kashivskyy) #4

Amir, your proposal contradicts "clarity over brevity" principle. As pointed by Liam all those things are fairly easy to do without any major complexity.

Pozdrawiam – Regards,
Adrian Kashivskyy

···

Wiadomość napisana przez Amir Michail via swift-evolution <swift-evolution@swift.org> w dniu 17.12.2015, o godz. 17:26:

Python examples:

l = [x*x for x in range(10)] // list comprehension

l2 = [(x,y) for x in range(10) for y in range(10) if x + y < 8] // another list comprehension

g = (x*x for x in range(10)) // generator comprehension

d = {x:x*x for x in range(10)} // dictionary comprehension

I think most programmers would like using these. They are concise and easy to understand.

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Jordan Rose) #5

-1 from me. I find list comprehensions, on average, roughly as concise as methods on SequenceType, and generally harder to understand as soon as you have more than one collection.

Jordan

···

On Dec 17, 2015, at 8:26 , Amir Michail via swift-evolution <swift-evolution@swift.org> wrote:

Python examples:

l = [x*x for x in range(10)] // list comprehension

l2 = [(x,y) for x in range(10) for y in range(10) if x + y < 8] // another list comprehension

g = (x*x for x in range(10)) // generator comprehension

d = {x:x*x for x in range(10)} // dictionary comprehension

I think most programmers would like using these. They are concise and easy to understand.

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Alex Popov) #6

Data of 1: I misunderstood the second comprehension until I saw it written in
Swift, and I do know about list comprehensions from Erlang.

I agree that this seems to contradict "clarity over brevity".

<br

···

Alex Popov Jr.

Principal iOS Developer | Shelfie

On Dec 17 2015, at 12:44 pm, Amir Michail via swift-evolution &lt;swift- evolution@swift.org&gt; wrote:
  

On Dec 17, 2015, at 3:27 PM, Adrian Kashivskyy &lt;[adrian.kashivskyy@me.com](mailto:adrian.kashivskyy@me.com)&gt; wrote:

Amir, your proposal contradicts "clarity over brevity" principle. As

pointed by Liam all those things are fairly easy to do without any major
complexity.

I think the comprehensions are very clear though.

Pozdrawiam – Regards,

Adrian Kashivskyy

Wiadomość napisana przez Amir Michail via swift-evolution &lt;[swift-

evolution@swift.org](mailto:swift-evolution@swift.org)&gt; w dniu 17.12.2015,
o godz. 17:26:

Python examples:
  
l = [x*x for x in range(10)] // list comprehension
  
l2 = [(x,y) for x in range(10) for y in range(10) if x + y &lt; 8] // another
list comprehension
  
g = (x*x for x in range(10)) // generator comprehension
  
d = {x:x*x for x in range(10)} // dictionary comprehension
  
I think most programmers would like using these. They are concise and easy to
understand.
  
_______________________________________________
swift-evolution mailing list
[swift-evolution@swift.org](mailto:swift-evolution@swift.org)
<https://lists.swift.org/mailman/listinfo/swift-evolution>

![](https://u2002410.ct.sendgrid.net/wf/open?upn=CmwAv3oRa0AH4Hd1bWC6X-
2BzbhPqo1YEo6mPHEujr90sblMts0lyHPlhqzvj-2Fp-2BC5OEMkczNuE6qNGof856voE-
2BZnswBliGIoLaS-2BjycYlE0gnxN5cGJ98YQMEUtmMgZFbBpSh1ddBJSm21Gb1SvtRV0
-2BUl5I9pgd4gfaSj3QH9liTjcXY0B-2FKLe-
2BGskz0t2ZkdLzgQYgLttL6QnIT6UrERxZgyT9dQfFRCqL1CVFzAU-3D)


(Amir Michail) #7

Amir, your proposal contradicts "clarity over brevity" principle. As pointed by Liam all those things are fairly easy to do without any major complexity.

I think the comprehensions are very clear though.

···

On Dec 17, 2015, at 3:27 PM, Adrian Kashivskyy <adrian.kashivskyy@me.com> wrote:

Pozdrawiam – Regards,
Adrian Kashivskyy

Wiadomość napisana przez Amir Michail via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> w dniu 17.12.2015, o godz. 17:26:

Python examples:

l = [x*x for x in range(10)] // list comprehension

l2 = [(x,y) for x in range(10) for y in range(10) if x + y < 8] // another list comprehension

g = (x*x for x in range(10)) // generator comprehension

d = {x:x*x for x in range(10)} // dictionary comprehension

I think most programmers would like using these. They are concise and easy to understand.

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution


(Adrian Kashivskyy) #8

Yup, I thought it zips them (creates pairs of (1, 1), (2, 2), (3, 3), etc.).

Pozdrawiam – Regards,
Adrian Kashivskyy

···

Wiadomość napisana przez Alex Popov via swift-evolution <swift-evolution@swift.org> w dniu 17.12.2015, o godz. 22:50:

Data of 1: I misunderstood the second comprehension until I saw it written in Swift, and I do know about list comprehensions from Erlang.

I agree that this seems to contradict "clarity over brevity".


Alex Popov Jr.
Principal iOS Developer | Shelfie

On Dec 17 2015, at 12:44 pm, Amir Michail via swift-evolution <swift-evolution@swift.org> wrote:

On Dec 17, 2015, at 3:27 PM, Adrian Kashivskyy <adrian.kashivskyy@me.com <mailto:adrian.kashivskyy@me.com>> wrote:

Amir, your proposal contradicts "clarity over brevity" principle. As pointed by Liam all those things are fairly easy to do without any major complexity.

I think the comprehensions are very clear though.

Pozdrawiam – Regards,
Adrian Kashivskyy

Wiadomość napisana przez Amir Michail via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> w dniu 17.12.2015, o godz. 17:26:

Python examples:

l = [x*x for x in range(10)] // list comprehension

l2 = [(x,y) for x in range(10) for y in range(10) if x + y < 8] // another list comprehension

g = (x*x for x in range(10)) // generator comprehension

d = {x:x*x for x in range(10)} // dictionary comprehension

I think most programmers would like using these. They are concise and easy to understand.

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Al Skipp) #9

Whether Comprehensions should be introduced to the language is worth consideration (Are they vital? Probably not). But I don’t find the Swift examples using ‘for' statements particularly compelling. The ‘for’ statement needs to declare a mutable variable which is updated from within the loop. It works, but it’s not particularly elegant.

var l2: [(Int, Int)] = []
for x in 1...10 {
  for y in 1...10 where (x + y) < 8 {
    l2.append((x, y))
  }
}

It could be achieved using an expression, but it’s pretty horrible too for several reasons:

let l3 = (1...10).flatMap { x in
  (1...10).map { y in (x,y) }
}.filter { (x, y) in x + y < 8 }

(Maybe there’s a more elegant solution?)

If Swift had Comprehensions, they probably wouldn’t look exactly like the example below, but in my opinion it’s superior to the 2 code samples above.

l2 = [(x,y) for x in range(10) for y in range(10) if x + y < 8]

Al

···

On 17 Dec 2015, at 20:25, Liam Butler-Lawrence via swift-evolution <swift-evolution@swift.org> wrote:

Comprehensions are great in Python. However, Swift can already do all of these things via for...in, map() and/or generate().


(TJ Usiyan) #10

Using a sequence meant to represent a [Cartesian Product](
https://github.com/griotspeak/CartesianProduct)

You can write this

        for (i, j) in (0...10) • (0...10) where i + j < 8 {

            print(i)

        }

I am not dead set on the • operator as cartesian product and only added it
back in to demonstrate that this can be *this* pretty.

TJ

···

On Thu, Dec 17, 2015 at 7:41 PM, Al Skipp via swift-evolution < swift-evolution@swift.org> wrote:

On 17 Dec 2015, at 20:25, Liam Butler-Lawrence via swift-evolution < > swift-evolution@swift.org> wrote:

Comprehensions are great in Python. However, Swift can already do all of
these things via for...in, map() and/or generate().

Whether Comprehensions should be introduced to the language is worth
consideration (Are they vital? Probably not). But I don’t find the Swift
examples using ‘for' statements particularly compelling. The ‘for’
statement needs to declare a mutable variable which is updated from within
the loop. It works, but it’s not particularly elegant.

var l2: [(Int, Int)] = []
for x in 1...10 {
  for y in 1...10 where (x + y) < 8 {
    l2.append((x, y))
  }
}

It could be achieved using an expression, but it’s pretty horrible too for
several reasons:

let l3 = (1...10).flatMap { x in
  (1...10).map { y in (x,y) }
}.filter { (x, y) in x + y < 8 }

(Maybe there’s a more elegant solution?)

If Swift had Comprehensions, they probably wouldn’t look exactly like the
example below, but in my opinion it’s superior to the 2 code samples above.

l2 = [(x,y) for x in range(10) for y in range(10) if x + y < 8]

Al

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution