contents

🤔 Swift

lazy

우리가 아는 lazy var말고도 LazySequence가 존재한다.

<aside> 🗂️ Swift 표준 라이브러리에서 SequenceType 및 Collection Type 프로토콜에는 lazy라는 연산 프로퍼티가 있으며, 이 클래스는 특수 LazySequence 또는 LazyCollection를 반환합니다.

</aside>

let overallRange = rangeOfRanges(data.lazy.map { $0[keyPath: path] })

Chaining lazy sequences

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

Being Lazy

Protocol