Avatar
norio_nomura 3/8/2018 2:54 AM
Arrayの代わりにUnsafeMutableBufferPointerとか使えば排他制御不要? extension Array { func concurrentMap<B>(_ transform: (Element) -> B) -> [B] { var result = ContiguousArray<B?>(repeating: nil, count: count) return result.withUnsafeMutableBufferPointer { buffer in DispatchQueue.concurrentPerform(iterations: buffer.count) { idx in buffer[idx] = transform(self[idx]) } return buffer.map { $0! } } } } ContiguousArrayに変更 (edited)