filter
returns an array which is being printed out by the playground.
/// Return a Array containing the elements `x` of `self` for which
/// `includeElement(x)` is `true`
func filter(includeElement: (T) -> Bool) -> T[]
I believe the (6 times)
is incorrect because if you look below it true
only gets returned 3 times.
EDIT: The above is incorrect.
From playing around with it more, I can only say that this is simply the behaviour of the filter
function.
letters.reverse().filter({
(x : String) -> Bool in
println("PRINT: \(x)")
return true
})
This prints CBACBA
so its simply always traversing the array in order, twice.
letters.filter({
(x : String) -> Bool in
println("PRINT: \(x)")
if (x == "A") {
return true
}
return false
})
This still prints ABCABC
, so go figure..
I'll go ask a Swift engineer in a bit and get back to you on why this is! (If they know :p)