Laziness in Swift
问题 Why is lazy used here? extension SequenceType { func mapSome<U>(transform: Generator.Element -> U?) -> [U] { var result: [U] = [] for case let x? in lazy(self).map(transform) { result.append(x) } return result } } this extension takes a transformation function that returns an optional, and returns an array of only those values that weren’t transformed into nil Why not just use self.map(transform) ? is laziness necessary here? 回答1: It avoids the creation of an intermediate array. self.map