I want to remove all elements of value x from an array that contains x, y and z elements
let arr = [\'a\', \'b\', \'c\', \'b\']
How can I r
A general approach is to exploit first class procedures. (However, this approach is much more powerful than what is required for your question.) To illustrate, say you want to avoid "Justin" repeatedly in many collections.
let avoidJustin = notEqualTester ("Justin")
let arrayOfUsers = // ...
arrayOfUsers.filter (avoidJustin)
let arrayOfFriends = // ...
arrayOfFriends.filter (avoidJustin)
With this, you avoid repeatedly creating a closure each time you want to avoid Justin. Here is notEqualTester
which, given a that
, returns a function of this
that returns this != that
.
func notEqualTester<T: Equatable> (that:T) -> ((this:T) -> Bool) {
return { (this:T) -> Bool in return this != that }
}
The returned closure for this
captures the value for that
- which can be useful when that
is no longer available.