Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does filters in Swift iterate the collection twice?

Tags:

swift

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?

like image 812
mythz Avatar asked Jun 03 '14 21:06

mythz


People also ask

Does filter modify the original array Swift?

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.

How do I filter an array of arrays in Swift?

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.

How do I filter a string in Swift?

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.


2 Answers

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.

like image 106
Analog File Avatar answered Nov 15 '22 20:11

Analog File


It is modified in beta 5. It is now traveling only once now, printing ABC instead of ABCABC

like image 42
onevcat Avatar answered Nov 15 '22 18:11

onevcat