Avatar
こっちの方がいい // (Element) -> Element? 版 extension Array { mutating func updateOrRemove(_ f: (Element) -> Element?) { var writeIdx = startIndex for elem in self { if let elem = f(elem) { self[writeIdx] = elem writeIdx = self.index(after: writeIdx) } } removeLast(endIndex - writeIdx) } } var ary = [1,2,3,4] ary.updateOrRemove { (elem) in if elem % 3 == 0 { return nil } return elem * 10 } print(ary)