swift - Sequence drop(while:) Seemingly Does Nothing -


the swift standard library asserts that:

drop(while:)

returns subsequence skipping elements while predicate returns true , returning remaining elements.

by using function signature:

func drop(while predicate: (self.element) throws -> bool) rethrows -> self.subsequence 

where predicate described as:

a closure takes element of sequence argument , returns boolean value indicating whether element match.

my problem that, under description following behavior shouldn't occur:

let test = (0...3).drop { $0 > 1 } test.contains(0) // true test.contains(3) // true 

i don't why don't understand behaviour. documentation pretty clear , matches output.

the documentation says method keep skipping (dropping) elements while predicate true. it's while loop:

// not real code, demonstration purposes while predicate(sequence.first) {     sequence.dropfirst() } 

and remaining sequence returned.

for sequence 0...3, it's [0, 1, 2] right?

does first element, 0, satisfy thee predicate $0 > 1? no, imaginary while loop breaks, nothing dropped, original sequence returned.

i think confusing maybe prefix?

with prefix, keep adding elements sequence while predicate true , return sequence when predicate becomes false.

let test = (0...3).prefix { $0 > 1 } test.contains(0) // false test.contains(3) // false 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -