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.
-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.
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.
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.
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().
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]
To my knowledge, there hasn't been any work on adding Python-style comprehensions in Swift. However, there has been work on other parts of Swift (such as improving the APIs for collection types and the introduction of Swift Algorithms) which practically eliminates the need for comprehensions. All four examples given can already be trivially written in Swift today.
import Algorithms
let l = (0..<10).map { x in x * x }
let l2 = product(0..<10, 0..<10).filter { x, y in x + y < 8 }
let g = (0..<10).lazy.map { x in x * x }
let d = Dictionary(uniqueKeysWithValues: (0..<10).lazy.map { x in (x, x * x) })
They really arenât, practically by definition. Theyâre tough to write due to the many operators with varied meanings and much harder to read for the same reason. Swift suffers a bit here due to the use of functional terms rather than plain English, but at least thereâs some knowledge transfer from other domains. Comprehensions are inscrutable to anyone who isnât already familiar with them.
and yet the population of people who learn python is enormous compared to the number of people who learn swift. so i think the âknowledge transferâ argument is one in favor of adding comprehensions to swift.
If we added every feature found in more popular languages, Swift wouldn't be Swift. It would be a Frankenstein's monster of those other languages.
The Core Team does look to other languages for inspiration and guidance for new features, because there's no value in coming up with new syntax or semantics out of some kind of NIH infection.
swift does add âevery feature found in more popular languagesâ. other languages have class inheritance, so we have class inheritance. other languages have type erasure, so we have Any and any. other languages have dynamic member lookup, so we have dynamic member lookup. other languages have macros, so it looks like we are getting macros.
none of these are things that i think should show up (frequently) in âgood qualityâ swift code. and yet the language supports them because these are things developers from other universes are used to, and it didnât, then swift would just continue existing in itâs own little bubble.