The language guide has revealed no trace of list comprehension. What's the neatest way of accomplishing this in Swift? I'm looking for something similar to:
evens = [ x for x in range(10) if x % 2 == 0]
The language guide has revealed no trace of list comprehension. What's the neatest way of accomplishing this in Swift? I'm looking for something similar to:
evens = [ x for x in range(10) if x % 2 == 0]
Generally, a list comprehension in Python can be written in the form:
Which is the same as
Therefore, in Swift you can write it as
For example:
As of Swift 2 you can do something like this:
Got to admit, I am surprised nobody mentioned flatmap, since I think it's the closest thing Swift has to list (or set or dict) comprehension.
Flatmap takes a closure, and you can either return individual values (in which case it will return an array with all of the non-nil values and discard the nils) or return array segments (in which case it will catenate all of your segments together and return that.)
Flatmap seems mostly (always?) to be unable to infer return values. Certainly, in this case it can't, so I specify it as -> Int? so that I can return nils, and thus discard the odd elements.
You can nest flatmaps if you like. And I find them much more intuitive (although obviously also a bit more limited) than the combination of map and filter. For example, the top answer's 'evens squared', using flatmap, becomes,
The syntax isn't quite as one-line not-quite-the-same-as-everything-else as python's is. I'm not sure if I like that less (because for the simple cases in python it's very short and still very readable) or more (because complex cases can get wildly out of control, and experienced python programmers often think that they're perfectly readable and maintainable when a beginner at the same company can take half an hour to puzzle out what it was intended to do, let alone what it's actually doing.)
Here is the version of flatMap from which you return single items or nil, and here is the version from which you return segments.
It's probably also worth looking over both array.map and array.forEach, because both of them are also quite handy.
One aspect of list comprehension that hasn't been mentioned in this thread is the fact that you can apply it to multiple lists' Cartesian product. Example in Python:
… or Haskell:
In Swift, the 2-list equivalent logic is
And we'd have to increase the nesting level as the number of lists in input increases.
I recently made a small library to solve this problem (if you consider it a problem). Following my first example, with the library we get
The rationale (and more about list comprehension in general) is explained in an blog post.
One way would be :
With Swift 3, according to your needs or tastes, you may choose one of the seven following Playground codes that are kind of equivalent to Python list comprehension.
#1. Using
stride(from:to:by:)
function#2. Using
CountableRange
filter(_:)
method#3. Using
CountableRange
flatMap(_:)
method#4. Using
sequence(first:next:)
function#5. Using
AnySequence
init(_:)
initializer#6. Using for loop with where clause
#7. Using for loop with if condition