The following code in Swift's playground or Console App:
let letters = ["A", "B", "C"]
letters.filter({
(x : String) -> Bool in
println("PRINT: \(x)")
return true
})
Prints out:
PRINT: A
PRINT: B
PRINT: C
PRINT: A
PRINT: B
PRINT: C
Why does it iterate over the collection twice?
When you apply a filter on an Array, the original Array is not modified. Instead, filter(isIncluded:) creates a new Array with only the elements you want.
To filter elements of a Swift Array, call filter() method on this array and pass the predicate/condition to the filter() method. filter() method returns a new array with the elements of the original array, that satisfy the given condition.
To filter strings in a Swift String Array based on length, call filter() method on this String Array, and pass the condition prepared with the string length as argument to the filter() method. filter() method returns an array with only those elements that satisfy the given predicate/condition.
Most probably filter
is implemented to first count the number of elements it needs to store and then, after using that number to size the allocation of storage for a new array, looping again to copy the ones he needs to keep.
The fact that it loops only once if you always return false
means that it optimizes away the second cycle iff the result is empty.
You may want to radar this as a bug but it probably is "working as designed": arrays are not lists, after all.
It is modified in beta 5. It is now traveling only once now, printing ABC
instead of ABCABC
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With