Let's say I have a struct…
struct Person {
let isMale: Bool
let name: String
}
and an array of Person
structs. I want to trim all the men (isMale == true
) from the start and end of the array (similarly to how you'd trim whitespace from the start & end of a string)…
func trimMen(people: [Person]) -> [Person]
var trimmedPeople: [Person] = people
while trimmedPeople.first?.isMale {
trimmedPeople.removeFirst()
}
while trimmedPeople.last?.isMale {
trimmedPeople.removeLast()
}
return trimmedPeople
}
Is there a more efficient way to do it?