{"id":957,"date":"2015-05-21T15:28:32","date_gmt":"2015-05-21T06:28:32","guid":{"rendered":"https:\/\/blog.ymyzk.com\/?p=957"},"modified":"2015-05-21T15:28:32","modified_gmt":"2015-05-21T06:28:32","slug":"swift-generator-lazy","status":"publish","type":"post","link":"https:\/\/blog.ymyzk.com\/2015\/05\/swift-generator-lazy\/","title":{"rendered":"Swift \u3067\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u3092\u4f5c\u3063\u305f\u308a\u3001\u9045\u5ef6\u8a55\u4fa1\u3057\u3066\u307f\u308b"},"content":{"rendered":"
Swift 1.2 \u3092\u4f7f\u3063\u3066\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u3092\u4f5c\u6210\u3057\u305f\u308a, \u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u3092\u4f7f\u3063\u3066\u9045\u5ef6\u8a55\u4fa1\u3092\u3057\u3066\u307f\u307e\u3059. \u540c\u69d8\u306e\u3053\u3068\u3092\u3053\u308c\u307e\u3067\u3044\u304f\u3064\u304b\u306e\u8a00\u8a9e\u3067\u884c\u3063\u3066\u3044\u307e\u3059\u304c, \u305d\u308c\u306b\u3064\u3044\u3066\u306f\u4ee5\u4e0b\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044.<\/p>\n
<\/p>\n
Swift \u3067\u306e\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u306f 0 \u304b\u3089 3 \u307e\u3067\u306e\u81ea\u7136\u6570\u3092\u8fd4\u3059\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u306e\u4f8b\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059.<\/p>\n Swift \u306e for-in \u6587\u306f \u5148\u307b\u3069\u306e Swift \u3067\u306f \u4f8b\u3068\u3057\u3066\u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u5217\u3092\u8fd4\u3059\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u3092\u4f5c\u6210\u3057\u307e\u3059. \u3053\u306e\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u306f\u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u3092 0 \u304b\u3089\u9806\u306b\u7121\u9650\u306b\u8fd4\u3057\u307e\u3059.<\/p>\n Swift \u3067\u306f \u4ee5\u4e0b\u306f, \u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u5217\u306e\u5404\u8981\u7d20\u3092\u4e8c\u4e57\u3057\u3066, \u5947\u6570\u3060\u3051\u306e\u6570\u5217\u3092\u4f5c\u308a, \u5148\u982d\u304b\u308910\u8981\u7d20\u3092\u9806\u306b\u8868\u793a\u3059\u308b\u4f8b\u3067\u3059.<\/p>\n Swift \u306e\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u7b49\u306b\u3064\u3044\u3066\u306f\u516c\u5f0f\u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306b\u3082\u3042\u307e\u308a\u60c5\u5831\u304c\u63b2\u8f09\u3055\u308c\u3066\u3044\u307e\u305b\u3093. \u305d\u308c\u305e\u308c\u306e\u578b\u3084\u95a2\u6570\u306e\u5b9a\u7fa9\u306b\u30b3\u30e1\u30f3\u30c8\u3067\u8aac\u660e\u304c\u8a18\u8ff0\u3055\u308c\u3066\u3044\u308b\u305f\u3081, \u305d\u308c\u3082\u5408\u308f\u305b\u3066\u78ba\u8a8d\u3059\u308b\u3053\u3068\u3092\u304a\u3059\u3059\u3081\u3057\u307e\u3059.<\/p>\nGeneratorType<\/code> \u30d7\u30ed\u30c8\u30b3\u30eb (protocol) \u306b\u9069\u5408\u3057\u3066\u3044\u308b\u578b\u3068\u3057\u3066\u8868\u3055\u308c\u307e\u3059.
GeneratorType<\/code> \u306e\u5b9a\u7fa9\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059.<\/p>\n
\r\nprotocol GeneratorType {\r\n typealias Element\r\n mutating func next() -> Element?\r\n}\r\n<\/pre>\n
\r\nstruct MyGenerator: GeneratorType {\r\n typealias Element = Int\r\n var i = 0\r\n mutating func next() -> Element? {\r\n if i <= 3 {\r\n return i++\r\n } else {\r\n return nil\r\n }\r\n }\r\n}\r\n\r\nvar g = MyGenerator()\r\ng.next() \/\/ 0\r\ng.next() \/\/ 1\r\ng.next() \/\/ 2\r\ng.next() \/\/ 3\r\ng.next() \/\/ nil\r\n<\/pre>\n
SequenceType<\/code> \u30d7\u30ed\u30c8\u30b3\u30eb (protocol) \u306b\u9069\u5408\u3057\u3066\u3044\u308b\u578b\u306e\u305d\u308c\u305e\u308c\u306e\u8981\u7d20\u306b\u3064\u3044\u3066\u51e6\u7406\u3092\u884c\u3046\u3053\u3068\u304c\u51fa\u6765\u307e\u3059.<\/p>\n
\r\nprotocol _SequenceType {\r\n}\r\n\r\nprotocol _Sequence_Type : _SequenceType {\r\n typealias Generator : GeneratorType\r\n func generate() -> Generator\r\n}\r\n\r\nprotocol SequenceType : _Sequence_Type {\r\n typealias Generator : GeneratorType\r\n func generate() -> Generator\r\n}\r\n<\/pre>\n
MyGenerator<\/code> \u3092\u4f7f\u3063\u3066\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u5b9a\u7fa9\u3057\u3066, for-in \u6587\u3067\u5229\u7528\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u307e\u3059.<\/p>\n
\r\nstruct MySequence: SequenceType {\r\n typealias Generator = MyGenerator\r\n func generate() -> Generator {\r\n return Generator()\r\n }\r\n}\r\n\r\nfor i in MySequence() {\r\n println(i) \/\/ 0, 1, 2, 3 \u304c\u9806\u306b\u8868\u793a\u3055\u308c\u307e\u3059\r\n}\r\n<\/pre>\n
SequenceType<\/code> \u306b\u9069\u5408\u3057\u305f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u5bfe\u3057\u3066,
map<\/code> \u3084
filter<\/code> \u3068\u3044\u3063\u305f\u95a2\u6570\u3092\u9069\u7528\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u307e\u3059.<\/p>\n
\r\nprintln(map(MySequence(), { $0 * 2 }))\r\n\/\/ [0, 2, 4, 6] \u304c\u8868\u793a\u3055\u308c\u307e\u3059\r\n\r\nprintln(filter(MySequence(), { $0 % 2 == 0 }))\r\n\/\/ [0, 2] \u304c\u8868\u793a\u3055\u308c\u307e\u3059\r\n<\/pre>\n
\u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u5217\u306e\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf<\/h2>\n
\r\nstruct FibonacciGenerator: GeneratorType {\r\n typealias Element = Int\r\n var (a, b) = (0, 1)\r\n mutating func next() -> Element? {\r\n let n = a\r\n (a, b) = (b, a + b)\r\n return n\r\n }\r\n}\r\n\r\nvar fib = FibonacciGenerator()\r\nfib.next() \/\/ 0\r\nfib.next() \/\/ 1\r\nfib.next() \/\/ 1\r\nfib.next() \/\/ 2\r\nfib.next() \/\/ 3\r\n\r\nstruct FibonacciSequence: SequenceType {\r\n typealias Generator = FibonacciGenerator\r\n func generate() -> Generator {\r\n return Generator()\r\n }\r\n}\r\n\r\n\/\/ \u30aa\u30fc\u30d0\u30fc\u30d5\u30ed\u30fc\u3059\u308b\u307e\u3067\u505c\u6b62\u3057\u306a\u3044\r\n\/\/for i in FibonacciSequence() {\r\n\/\/ println(i)\r\n\/\/}\r\n\r\nfor (i, n) in zip(0..<10, FibonacciSequence()) {\r\n println(\"F_\\(i) = \\(n)\") \/\/ F_0 = 0 \u304b\u3089 F_9 = 34 \u307e\u3067\u9806\u306b\u8868\u793a\u3055\u308c\u307e\u3059.\r\n}\r\n<\/pre>\n
\u9045\u5ef6\u8a55\u4fa1<\/h2>\n
SequenceType<\/code> \u306b\u9069\u5408\u3057\u305f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304b\u3089,
LazySequence<\/code> \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u8fd4\u3059
lazy<\/code> \u95a2\u6570\u304c\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u307e\u3059.<\/p>\n
LazySequence<\/code> \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u306f
LazySequence<\/code> \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u8fd4\u3059,
map<\/code> \u3068
filter<\/code> \u30e1\u30bd\u30c3\u30c9\u304c\u5b9f\u88c5\u3055\u308c\u3066\u3044\u307e\u3059. \u3053\u308c\u3092\u7528\u3044\u3066\u6f14\u7b97\u3092\u9045\u5ef6\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059.<\/p>\n
\r\nlet fib2 = lazy(FibonacciSequence()).map({ $0 * $0 }).filter({ $0 % 2 == 1 })\r\nfor (i, n) in zip(0..<10, fib2) {\r\n println(n) \/\/ \u9806\u306b 1, 1, 9, 25, ..., 142129 \u304c\u8868\u793a\u3055\u308c\u307e\u3059\r\n}\r\n<\/pre>\n
\u53c2\u8003<\/h2>\n