Sequence.filter(_:) vs Array.drop(while:)

Hello, I was wondering if there are any differences between filter(_:) and drop(while:). I know the return types are different but I would like to know about the underlying algorithms and the performance differences. Thanks!

filter(_:) processes the whole Collection, while drop(while:) drops only the start.

let source = [1, 1, 1, 2, 2, 2, 3, 3, 3]

let a = source.filter { $0.isMultiple(of: 2) }
let b = source.drop(while: { !$0.isMultiple(of: 2) })

print(Array(a), Array(b))

will print [2, 2, 2] [2, 2, 2, 3, 3, 3]

1 Like

It makes sense now. I was not using the correct examples in my playground therefore I was getting the same result from each method. Yours made it clear immediately. Thank you!

1 Like