[swift-evolution] Proposal: Python's list, generator, and dictionary comprehensions

Liam Butler-Lawrence liamdunn at me.com
Thu Dec 17 14:25:20 CST 2015


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 at 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 at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151217/32783daa/attachment.html>


More information about the swift-evolution mailing list