Hi all, can someone explain me while in this playground code
import Foundation
let arr:Array<Int> = [1,3,5,7,9,11]
let exc:Array<Int> = [3,7,11]
let newarr = arr.drop { exc.contains($0) }
let newarr2 = arr.filter { !exc.contains($0) }
print(newarr)
print(newarr2)
the drop function return the whole array (don't drop anything) while the filter one give the right result? Thank you
That's how drop works.
Doc explains how closure works:
A closure that takes an element of the sequence as its argument and returns true if the element should be skipped or false if it should be included. Once the predicate returns false it will not be called again.
So, in your case, the first test on 1 fails and then drop stops.
If you change to:
let arr:Array<Int> = [3,5,7,9,11]
let exc:Array<Int> = [3,7,11]
you get
[5, 7, 9, 11]
[5, 9]
as it fails on 5
With
let arr:Array<Int> = [3,7,9,11]
let exc:Array<Int> = [3,7,11]
you get
[9, 11]
[9]
as it fails at 9