contents
우리가 아는 lazy var말고도 LazySequence가 존재한다.
<aside> 🗂️ Swift 표준 라이브러리에서 SequenceType 및 Collection Type 프로토콜에는 lazy라는 연산 프로퍼티가 있으며, 이 클래스는 특수 LazySequence 또는 LazyCollection를 반환합니다.
</aside>
let overallRange = rangeOfRanges(data.lazy.map { $0[keyPath: path] })
map, flatMap, filter 같은 고차함수에서 사용 가능하다.
lazy를 사용하지 않은 예시
lazy 사용 예시
func increment(x: Int) -> Int {
print("Computing next value of \\(x)")
return x+1
}
func double(x: Int) -> Int {
print("Computing double value of \\(x)…")
return 2*x
}
let doubleArray = array.lazy.map(increment).map(double)
print(doubleArray[3])
| 진행 순서 | 결과 |
|---|---|
| print(doubleArray[3]) | doubleArray 연산 시작 |
| doubleArray[3] | 3 |
| increment(x: 3) | 4 |
| double(x: 4) | 8 |