Avatar
inout 版はクライアント側の使い勝手が微妙ですねぇ。 // (inout Element?) -> Void 版 extension Array { mutating func updateOrRemove(_ f: (inout Element?) -> Void) { var writeIdx = startIndex for readIdx in indices { var elem: Element? = self[readIdx] f(&elem) if let elem = elem { self[writeIdx] = elem writeIdx = self.index(after: writeIdx) } } removeLast(distance(from: writeIdx, to: endIndex)) } } var ary = [1,2,3,4] ary.updateOrRemove { (elem) in if elem! % 3 == 0 { elem = nil return } elem! *= 10 } print(ary)