{"id":876,"date":"2015-04-23T14:49:43","date_gmt":"2015-04-23T05:49:43","guid":{"rendered":"https:\/\/blog.ymyzk.com\/?p=876"},"modified":"2015-04-26T14:23:16","modified_gmt":"2015-04-26T05:23:16","slug":"python-generator-lazy","status":"publish","type":"post","link":"https:\/\/blog.ymyzk.com\/2015\/04\/python-generator-lazy\/","title":{"rendered":"Python \u3067\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u3092\u4f5c\u3063\u305f\u308a\u3001\u9045\u5ef6\u8a55\u4fa1\u3057\u3066\u307f\u308b"},"content":{"rendered":"

\u300cRuby \u306e Enumerator \u3067\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u3092\u4f5c\u3063\u305f\u308a\u3001\u9045\u5ef6\u8a55\u4fa1\u3057\u3066\u307f\u308b<\/a>\u300d\u3068\u3044\u3046\u8a18\u4e8b\u3092\u898b\u304b\u3051\u305f\u306e\u3067 Python 2 \/ 3 \u3067\u66f8\u304f\u3068\u3069\u306e\u3088\u3046\u306b\u306a\u308b\u304b, \u5b9f\u969b\u306b\u66f8\u3044\u3066\u307f\u308b\u3053\u3068\u306b\u3057\u307e\u3057\u305f.<\/p>\n

<\/p>\n

\u30a4\u30c6\u30ec\u30fc\u30bf\u30fb\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u306e\u57fa\u672c<\/h2>\n

Python \u306b\u304a\u3051\u308b\u30a4\u30c6\u30ec\u30fc\u30bf\u3084\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u306b\u3064\u3044\u3066\u3053\u3053\u3067\u8a73\u7d30\u306f\u8aac\u660e\u3057\u306a\u3044\u306e\u3067\u300c\u30af\u30ed\u30fc\u30b8\u30e3\u3068\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf – Dive Into Python 3 \u65e5\u672c\u8a9e\u7248<\/a>\u300d\u3084\u300c\u30af\u30e9\u30b9\u3068\u30a4\u30c6\u30ec\u30fc\u30bf – Dive Into Python 3 \u65e5\u672c\u8a9e\u7248<\/a>\u300d\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044.<\/p>\n

\r\nfrom __future__ import print_function\r\n\r\n\r\ndef generator():\r\n    yield 1\r\n    yield 2\r\n    yield 3\r\n\r\n\r\n# Python 2 \u306e\u5834\u5408\r\ng = generator()\r\ng.next()  # 1\r\ng.next()  # 2\r\ng.next()  # 3\r\ng.next()  # Exception: StopIteration\r\n\r\n# Python 3 \u306e\u5834\u5408\r\ng = generator()\r\nnext(g)  # 1\r\nnext(g)  # 2\r\nnext(g)  # 3\r\nnext(g)  # Exception: StopIteration\r\n\r\n\r\nfor i in generator():\r\n    print(i)  # \u9806\u306b 1, 2, 3 \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

\u4f8b\u3068\u3057\u3066\u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u5217\u306e\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u3092\u4f5c\u6210\u3057\u307e\u3059. fib_generator<\/code> \u306f\u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u5217\u3092\u7121\u9650\u306b\u8fd4\u3059\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u3067\u3059.<\/p>\n

\r\nfrom __future__ import print_function\r\nfrom itertools import islice\r\n\r\n\r\ndef fib_generator():\r\n    a, b = 0, 1\r\n    while True:\r\n        yield a\r\n        a, b = b, a + b\r\n\r\n\r\n# Python 2 \u306e\u5834\u5408\r\nfib = fib_generator()\r\nfib.next()  # 0\r\nfib.next()  # 1\r\nfib.next()  # 1\r\nfib.next()  # 2\r\nfib.next()  # 3\r\n\r\n# Python 3 \u306e\u5834\u5408\r\nfib = fib_generator()\r\nnext(fib)  # 0\r\nnext(fib)  # 1\r\nnext(fib)  # 1\r\nnext(fib)  # 2\r\nnext(fib)  # 3\r\n\r\nislice(fib_generator(), 10)  # <itertools.islice at 0x...>\r\nlist(islice(fib_generator(), 10))  # [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]\r\n<\/pre>\n

12-17\u884c\u76ee\u306f\u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u5217\u3092 $latex F_0$ \u304b\u3089 $latex F_4$ \u307e\u3067\u9806\u306b\u53d6\u5f97\u3059\u308b\u4f8b\u3067\u3059. 20-21\u884c\u76ee\u306f\u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u5217\u306e\u5148\u982d\u304b\u308910\u500b\u8981\u7d20\u3092\u53d6\u5f97\u3059\u308b\u4f8b\u3067\u3059. islice<\/code> \u306f\u30a4\u30bf\u30ec\u30fc\u30bf\u3092\u8fd4\u3059\u306e\u3067 list<\/code> \u95a2\u6570\u3067 list<\/code> \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u5909\u63db\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059.<\/p>\n

\u9045\u5ef6\u8a55\u4fa1<\/h2>\n

Python \u3067 yield<\/code> \u3092\u4f7f\u3063\u305f\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u3092\u4f5c\u6210\u3059\u308b\u3068\u9045\u5ef6\u8a55\u4fa1\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u306f\u3053\u308c\u307e\u3067\u306e\u4f8b\u304b\u3089\u5206\u304b\u308b\u3053\u3068\u3068\u601d\u3044\u307e\u3059. \u30a4\u30c6\u30ec\u30fc\u30bf\u3092\u5f15\u6570\u306b\u3068\u3063\u3066\u30a4\u30c6\u30ec\u30fc\u30bf\u3092\u8fd4\u3059\u95a2\u6570\u3092\u5229\u7528\u3059\u308b\u3053\u3068\u3067, \u3088\u308a\u8907\u96d1\u306a\u51e6\u7406\u3092\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059. \u3053\u306e\u3088\u3046\u306a\u30a4\u30c6\u30ec\u30fc\u30bf\u3092\u751f\u6210\u3059\u308b\u95a2\u6570\u306f\u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea\u306e itertools<\/code> \u3067\u591a\u6570\u63d0\u4f9b\u3055\u308c\u3066\u3044\u307e\u3059.<\/p>\n

\u6b21\u306f\u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u5217\u306e\u8981\u7d20\u3092\u305d\u308c\u305e\u308c2\u4e57\u3057\u305f\u6570\u5217\u304b\u3089, \u5947\u6570\u306e\u8981\u7d20\u3060\u3051\u306e\u6570\u5217\u3092\u4f5c\u308a, \u5148\u982d\u304b\u308910\u500b\u53d6\u5f97\u3059\u308b\u4f8b\u3067\u3059.<\/p>\n

\r\nfrom itertools import islice\r\n\r\n\r\ndef fib_generator():\r\n    a, b = 0, 1\r\n    while True:\r\n        yield a\r\n        a, b = b, a + b\r\n\r\n\r\nsq = lambda x: x ** 2\r\nodd = lambda x: x % 2 == 1\r\n\r\nislice(filter(odd, map(sq, fib_generator())), 10)  # \r\nlist(islice(filter(odd, map(sq, fib_generator())), 10))  # [1, 1, 9, 25, 169, 441, 3025, 7921, 54289, 142129]\r\n<\/pre>\n

\u307e\u305f Python 2 \u3067\u306f map<\/code>, filter<\/code> \u306a\u3069\u304c\u30a4\u30c6\u30ec\u30fc\u30bf\u3067\u306f\u306a\u304f\u30ea\u30b9\u30c8\u3092\u8fd4\u3059\u305f\u3081, \u4ee3\u308f\u308a\u306b\u30a4\u30c6\u30ec\u30fc\u30bf\u3092\u8fd4\u3059 itertools.imap<\/code> \u3084 itertools.ifilter<\/code> \u3092\u5229\u7528\u3057\u307e\u3059.<\/p>\n

\r\nfrom itertools import ifilter, imap, islice\r\n\r\n\r\ndef fib_generator():\r\n    a, b = 0, 1\r\n    while True:\r\n        yield a\r\n        a, b = b, a + b\r\n\r\n\r\nsq = lambda x: x ** 2\r\nodd = lambda x: x % 2 == 1\r\n\r\nislice(ifilter(odd, imap(sq, fib_generator())), 10)  # \r\nlist(islice(ifilter(odd, imap(sq, fib_generator())), 10))  # [1, 1, 9, 25, 169, 441, 3025, 7921, 54289, 142129]\r\n<\/pre>\n

\u307e\u3068\u3081<\/h2>\n

\u5192\u982d\u3067\u793a\u3057\u305f Ruby \u3067\u306e\u8a18\u4e8b\u3068\u6bd4\u8f03\u3057\u3066 Python \u3068 Ruby \u306e\u8003\u3048\u65b9\u306e\u9055\u3044\u304c\u57a3\u9593\u898b\u3048\u308b\u3068\u601d\u3044\u307e\u3059.<\/p>\n

Python \u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea\u306e itertools \u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306b\u306f, \u69d8\u3005\u306a\u30a4\u30c6\u30ec\u30fc\u30bf\u3092\u751f\u6210\u3059\u308b\u95a2\u6570\u3084\u4f7f\u7528\u4f8b\u304c\u307e\u3068\u3081\u3089\u308c\u3066\u3044\u308b\u306e\u3067, \u8208\u5473\u306e\u3042\u308b\u65b9\u306f\u4e00\u5ea6\u76ee\u3092\u901a\u3059\u3068\u826f\u3044\u3067\u3059.<\/p>\n

\u53c2\u8003<\/h2>\n