jonr
jonr

Reputation: 1163

What are the semantics of a where clause in for-in loop in swift?

The swift 2.1 language reference says the for-in loop can contain a where-clause. What are the semantics of a for-in loop that uses a where clause?

2.1 reference https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/loop-statement

Edit: to be clear I am asking in a public forum so that the semantics of the swift language can be addressed. The swift reference is incomplete, and while its semantics can be observed by playing with code it is in the best interest of the community to have a documented source for its behavior.

Upvotes: 2

Views: 1290

Answers (4)

Kubba
Kubba

Reputation: 3438

Are you asking about how does it work? It executes code block only for values that pass where condition.

These do the same (with one exception):

//1
for value in boolArray.filter({ $0 == true }){
    print(value)
}

//2
for value in boolArray where value == true {
    print(value)
}

Second option is faster - how much - it depends on your data. After some tests, second option is equivalent to this:

//3
for value in boolArray {
    if value == true {
        print(value)
    }
}

Upvotes: 1

vrwim
vrwim

Reputation: 14320

As can be seen on the page you linked, the syntax for the for-in loop with a where clause is like so:

for-in-statement → for ­case­opt ­pattern ­in expression­ where-clause­opt ­code-block­

So you could use it to loop over an array with a condition. For example you could do:

for element in array where element.someBoolValue {
    print(element)
}

instead of

for element in array {
    if element.someBoolValue {
        print(element)
    }
}

Another way to visualise it is like this:

for element in array.filter({$0.someBoolValue}) {
    print(element)
}

All these examples are functionally identical.

I hope you understand how the for-in loop is constructed better now.

Upvotes: 1

Sandeep
Sandeep

Reputation: 21144

Here are two simple example of how you could use it.

Lets suppose that you have two different structs Person and Address declared as follows:

struct Address {
    let country: String?
}
struct Person {
    let name: String
    let address: Address?
}

let people = [
    Person(name: "Jack", address: nil),
    Person(name: "John", address: Address(country: "Finland")),
    Person(name: "Jill", address: Address(country: "Nepal"))
]

For first example, you can iterate only those which match certain criteria like below,

for aPerson in people where aPerson.name == "Jack" {
    print(aPerson)
}

Notice that address is a optional, you can still use where clause to let it evaluate optional and iterate only those cases like this,

for aPerson in people where aPerson.address?.country == "Finland" {
    print(aPerson)
}

Here are other trivial examples on numbers,

let numbers = Array(1...100)

for number in numbers where number % 2 == 0 {
    print(number) // prints all even numbers
} 

Upvotes: 3

Eric Aya
Eric Aya

Reputation: 70098

If you're curious about the syntax, a simple example could be:

let numbers = [1, 42, 18, 73, 0]

for number in numbers where number > 20 {
    print(number)
}

Prints:

42
73

With a where clause, when looping, the code will skip the objects which don't match the clause.

Upvotes: 1

Related Questions