for

A for loop churns through a collection of elements. Pattern matching can act like a filter:

let groupSizes = [1, 5, 4, 6, 2, 1, 3] for case 1 in groupSizes {

print("Found an individual") // 2 times

}

In this example, the array provides a list of workgroup sizes for a school classroom. The implementation of the loop only runs for elements in the array that match the value 1. Since students in the class are encouraged to work in teams instead of by themselves, you can isolate the people who have not found a partner.

results matching ""

    No results matching ""