drop(where:) in swift

why this func exist?


what is high order function?

In mathematics and computer science, a higher-order function (also functional, functional form or functor) is a function that does at least one of the following:
takes one or more functions as arguments (i.e., procedural parameters),
returns a function as its result.[disputed — discuss]

there are so many high order function introduced in swift, such as map, flatMap, reduce, filter… etc

But drop, works a little differently with what you are expecting.

Instance Method
drop(while:)
Returns a subsequence by skipping the initial, consecutive elements that satisfy the given predicate.

Consecutive is key here,

let numbers = [3, 7, 4, -2, 9, -6, 10, 1]
let startingWithNegative = numbers.drop(while: { $0 > 0 })
// startingWithNegative == [-2, 9, -6, 10, 1]

Just remember this, drop will not drop all the elements that match the predicate. It will only keep evaluate to elemet util the one dose not satisfy it, then drop will stop working, return the rest of array from that index.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.