Skip to content

Instantly share code, notes, and snippets.

@ezura
Created December 17, 2015 13:14
Show Gist options
  • Save ezura/b282f431c11c337a088c to your computer and use it in GitHub Desktop.
Save ezura/b282f431c11c337a088c to your computer and use it in GitHub Desktop.
要素に必ずしも実体が必要ないことの説明 (アクセスしたとき値ができる)
struct SampleStruct {}
extension SampleStruct : SequenceType {
typealias Generator = SampleGenerator
func generate() -> Generator {
return Generator()
}
}
struct SampleGenerator : GeneratorType {
typealias Element = Int
var index = 0
mutating func next() -> Element? {
index++
if (index > 10) { return nil }
return Int(arc4random())
}
}
let sampleStruct = SampleStruct()
sampleStruct.map { $0 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment