{"id":892,"date":"2015-04-28T13:23:53","date_gmt":"2015-04-28T04:23:53","guid":{"rendered":"https:\/\/blog.ymyzk.com\/?p=892"},"modified":"2015-04-28T14:01:32","modified_gmt":"2015-04-28T05:01:32","slug":"ecmascript-6-generator-lazy","status":"publish","type":"post","link":"https:\/\/blog.ymyzk.com\/2015\/04\/ecmascript-6-generator-lazy\/","title":{"rendered":"ECMAScript 6 \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\u3084\u300cPython \u3067\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u3092\u4f5c\u3063\u305f\u308a\u3001\u9045\u5ef6\u8a55\u4fa1\u3057\u3066\u307f\u308b<\/a>\u300d\u306e\u8a18\u4e8b\u3092 ECMAScript 6 \u306e\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u3092\u4f7f\u3063\u3066\u8a18\u8ff0\u3059\u308b\u3068\u3069\u306e\u3088\u3046\u306b\u306a\u308b\u306e\u304b, \u5b9f\u969b\u306b\u8a66\u3057\u3066\u307f\u307e\u3057\u305f.<\/p>\n <\/p>\n ECMAScript 6 \u306e\u5404\u30d6\u30e9\u30a6\u30b6\u3084\u51e6\u7406\u7cfb\u3067\u306e\u5b9f\u88c5\u72b6\u6cc1\u306e\u8a73\u7d30\u306f\u3053\u3061\u3089<\/a>\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044.<\/p>\n \u73fe\u6642\u70b9\u3067\u3053\u306e\u8a18\u4e8b\u306e\u30b3\u30fc\u30c9\u304c\u305d\u306e\u307e\u307e\u5b9f\u884c\u3067\u304d\u308b\u51e6\u7406\u7cfb\u306f\u306a\u3044\u3068\u601d\u308f\u308c\u307e\u3059. \u30b3\u30fc\u30c9\u3092 Babel \u7b49\u3092\u7528\u3044\u3066 ECMAScript 5 \u306e\u30b3\u30fc\u30c9\u306b\u5909\u63db\u3059\u308b\u304b (\u3053\u306e\u5834\u5408\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u3092\u5229\u7528\u3059\u308b\u305f\u3081\u306b Polyfill<\/a> \u304c\u5fc5\u8981\u306b\u306a\u308a\u307e\u3059) arrow functions \u306a\u3069\u306e\u51e6\u7406\u7cfb\u304c\u5bfe\u5fdc\u3057\u3066\u3044\u306a\u3044\u90e8\u5206\u3092\u66f8\u304d\u63db\u3048\u3066\u5b9f\u884c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059.<\/p>\n ECMAScript 6 \u3067\u63d0\u6848\u3055\u308c\u3066\u3044\u308b\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u306b\u3064\u3044\u3066\u306f\u300cfunction* – JavaScript | MDN<\/a>\u300d\u306a\u3069\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044.<\/p>\n \u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u306e\u66f8\u304d\u65b9\u306f Python \u306e\u3082\u306e\u3068\u975e\u5e38\u306b\u3088\u304f\u4f3c\u3066\u3044\u307e\u3059\u304c, \u901a\u5e38\u306e\u95a2\u6570\u306f \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. Python \u306e\u5834\u5408\u306f\u3053\u306e\u3088\u3046\u306a\u51e6\u7406\u306f \u5b9f\u969b\u306e\u4f8b\u3092\u4ee5\u4e0b\u306b\u793a\u3057\u307e\u3059.<\/p>\n 50-52 \u884c\u76ee\u306e\u4f8b\u3067\u306f\u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u5217\u306e\u5404\u8981\u7d20\u30922\u4e57\u3057\u305f\u3082\u306e\u3092, \u5148\u982d\u304b\u308910\u500b\u53d6\u5f97\u3059\u308b\u4f8b\u3067\u3059. \u307e\u305a\u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u5217\u3092\u8fd4\u3059\u30a4\u30c6\u30ec\u30fc\u30bf\u3092\u4f5c\u308a, \u305d\u3053\u304b\u3089 55-57 \u884c\u76ee\u306e\u4f8b\u306f\u3082\u3046\u5c11\u3057\u8907\u96d1\u306a\u4f8b\u3067, \u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u5217\u306e\u5404\u8981\u7d20\u30922\u4e57\u3057\u305f\u3082\u306e\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 \u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u304c\u8fd4\u3059\u30a4\u30c6\u30ec\u30fc\u30bf\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f \u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u306b\u3064\u3044\u3066\u306e\u30af\u30e9\u30b9\u56f3<\/a>\u3092\u898b\u308b\u3068, \u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u304c\u8fd4\u3059\u30a4\u30c6\u30ec\u30fc\u30bf\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f \u3057\u304b\u3057 \u3053\u306e\u8a18\u4e8b\u306e\u5185\u5bb9\u306b\u3064\u3044\u3066\u691c\u8a0e\u3059\u308b\u306b\u3042\u305f\u308a @tyage<\/a> \u306b\u52a9\u8a00\u3092\u9802\u304d\u307e\u3057\u305f. \u611f\u8b1d.<\/p>\n \u300cRuby \u306e Enumerator \u3067\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u3092\u4f5c\u3063\u305f\u308a\u3001\u9045\u5ef6\u8a55\u4fa1\u3057\u3066\u307f\u308b\u300d\u3084\u300cPython \u3067\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u3092\u4f5c\u3063\u305f\u308a\u3001\u9045\u5ef6\u8a55\u4fa1\u3057\u3066\u307f\u308b\u300d\u306e\u8a18\u4e8b\u3092 ECMAScript 6 \u306e\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u3092\u4f7f\u3063\u3066\u8a18\u8ff0\u3059\u308b\u3068\u3069\u306e\u3088\u3046 […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[74,75,8],"_links":{"self":[{"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/posts\/892"}],"collection":[{"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/comments?post=892"}],"version-history":[{"count":22,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/posts\/892\/revisions"}],"predecessor-version":[{"id":912,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/posts\/892\/revisions\/912"}],"wp:attachment":[{"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/media?parent=892"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/categories?post=892"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/tags?post=892"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}ECMAScript 6<\/h2>\n
\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u306e\u57fa\u672c<\/h2>\n
function<\/code> \u3067\u8a18\u8ff0\u3059\u308b\u306e\u306b\u5bfe\u3057, \u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u306f
function*<\/code> \u3067\u8a18\u8ff0\u3057\u307e\u3059. (Python \u306e\u5834\u5408\u306f\u3069\u3061\u3089\u3082
def<\/code> \u3067\u826f\u3044.)<\/p>\n
\r\n(function() {\r\n \"use strict\";\r\n\r\n function* generator() {\r\n yield 1;\r\n yield 2;\r\n yield 3;\r\n }\r\n\r\n var g = generator();\r\n g.next(); \/\/ { value: 1, done: false }\r\n g.next(); \/\/ { value: 2, done: false }\r\n g.next(); \/\/ { value: 3, done: false }\r\n g.next(); \/\/ { value: undefined, done: true }\r\n\r\n\r\n for (let i of generator()) {\r\n console.log(i); \/\/ \u9806\u306b 1, 2, 3 \u304c\u8868\u793a\u3055\u308c\u307e\u3059\r\n }\r\n})();\r\n<\/pre>\n
\u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u5217\u306e\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf<\/h2>\n
fib_generator<\/code> \u306f\u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u5217\u3092\u7121\u9650\u306b\u8fd4\u3059\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u3067\u3059.<\/p>\n
\r\n(function() {\r\n \"use strict\";\r\n\r\n var fib_generator = function* () {\r\n var a = 0, b = 1;\r\n while (true) {\r\n yield a;\r\n [a, b] = [b, a + b];\r\n }\r\n };\r\n\r\n var fib = fib_generator();\r\n console.log(fib.next()); \/\/ { value: 0, done: false }\r\n console.log(fib.next()); \/\/ { value: 1, done: false }\r\n console.log(fib.next()); \/\/ { value: 1, done: false }\r\n console.log(fib.next()); \/\/ { value: 2, done: false }\r\n console.log(fib.next()); \/\/ { value: 3, done: false }\r\n})();<\/pre>\n
\u9045\u5ef6\u8a55\u4fa1<\/h2>\n
yield<\/code> \u3092\u4f7f\u3063\u305f\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u3092\u7528\u3044\u308b\u3068\u9045\u5ef6\u8a55\u4fa1\u3092\u5b9f\u73fe\u3067\u304d\u308b\u3053\u3068\u306f, \u3053\u308c\u307e\u3067\u306e\u4f8b\u304b\u3089\u5206\u304b\u308b\u3053\u3068\u3068\u601d\u3044\u307e\u3059. \u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u3092\u547c\u3073\u51fa\u3059\u3068\u30a4\u30c6\u30ec\u30fc\u30bf\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u8fd4\u3055\u308c\u307e\u3059\u304c, \u30a4\u30c6\u30ec\u30fc\u30bf\u306b\u5bfe\u3057\u3066\u51e6\u7406\u3092\u884c\u3044\u305d\u306e\u7d50\u679c\u3092\u30a4\u30c6\u30ec\u30fc\u30bf\u3068\u3057\u3066\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.<\/p>\n
itertools<\/code> \u3067\u3053\u306e\u3088\u3046\u306a\u95a2\u6570\u304c\u63d0\u4f9b\u3055\u308c\u3066\u3044\u307e\u3059\u304c, ECMAScript \u306b\u306f\u305d\u308c\u306b\u5bfe\u5fdc\u3059\u308b\u3082\u306e\u306f\u6a19\u6e96\u3067\u5b58\u5728\u3057\u307e\u305b\u3093. \u985e\u4f3c\u306e\u3082\u306e\u306b
Array.prototype.filter<\/code> \u3084
Array.prototype.map<\/code> \u304c\u3042\u308a, \u3053\u308c\u3068\u540c\u69d8\u306e\u64cd\u4f5c\u3092\u30a4\u30c6\u30ec\u30fc\u30bf\u306b\u304a\u3044\u3066\u3082\u6a21\u5023\u3057\u3066\u307f\u305f\u3044\u3068\u601d\u3044\u307e\u3059.<\/p>\n
\r\n(function() {\r\n \"use strict\";\r\n\r\n var GeneratorFunction = ((function* () {})()).constructor;\r\n\r\n GeneratorFunction.prototype.map = function* (callback, thisArg) {\r\n var i = 0;\r\n var o = Object(this);\r\n for (let item of this) {\r\n yield callback.call(thisArg, item, i, o);\r\n i++;\r\n }\r\n };\r\n\r\n GeneratorFunction.prototype.filter = function* (callback, thisArg) {\r\n var i = 0;\r\n var o = Object(this);\r\n for (let item of this) {\r\n if (callback.call(thisArg, item, i, o)) {\r\n yield item;\r\n }\r\n i++;\r\n }\r\n };\r\n\r\n GeneratorFunction.prototype.take = function* (n) {\r\n for (let item of this) {\r\n if (n <= 0) {\r\n break;\r\n }\r\n yield item;\r\n n--;\r\n }\r\n };\r\n\r\n var fib_generator = function* () {\r\n var a = 0, b = 1;\r\n while (true) {\r\n yield a;\r\n [a, b] = [b, a + b];\r\n }\r\n };\r\n\r\n \/\/ console.log(Array.from(fib_generator()));\r\n \/\/ \u505c\u6b62\u3057\u306a\u3044\r\n\r\n console.log(Array.from(fib_generator().take(10)));\r\n \/\/ [ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ]\r\n\r\n console.log(\r\n Array.from(fib_generator().map(x => Math.pow(x, 2)).take(10)\r\n ));\r\n \/\/ [ 0, 1, 1, 4, 9, 25, 64, 169, 441, 1156 ]\r\n\r\n console.log(\r\n Array.from(fib_generator().map(x => Math.pow(x, 2)).filter(x => x % 2 === 1).take(10)\r\n ));\r\n \/\/ [ 1, 1, 9, 25, 169, 441, 3025, 7921, 54289, 142129 ]\r\n})();\r\n<\/pre>\n
map<\/code> \u3092\u7528\u3044\u3066\u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u5217\u306e\u5404\u8981\u7d20\u30922\u4e57\u3057\u305f\u3082\u306e\u3092\u8fd4\u3059\u30a4\u30c6\u30ec\u30fc\u30bf\u3092\u751f\u6210\u3057\u307e\u3059, \u3055\u3089\u306b\u30a4\u30c6\u30ec\u30fc\u30bf\u306e\u5148\u982d\u304b\u308910\u8981\u7d20\u3060\u3051\u8fd4\u3059\u30a4\u30c6\u30ec\u30fc\u30bf\u3092\u751f\u6210\u3057\u307e\u3059. \u6700\u5f8c\u306b
Array.from<\/code> \u3067
Array<\/code> \u306b\u5909\u63db\u3057\u3066\u304b\u3089\u8868\u793a\u3057\u3066\u3044\u307e\u3059.<\/p>\n
map<\/code> \u3084
filter<\/code> \u3092\u5b9f\u88c5\u3057\u3066\u3044\u306a\u3044\u305f\u3081, \u3053\u308c\u3089\u306f 6-34 \u884c\u76ee\u3067\u72ec\u81ea\u306b\u5b9f\u88c5\u3057\u3066\u3044\u307e\u3059. \u3053\u3053\u3067\u306e
map<\/code> \u3084
filter<\/code> \u306e\u5b9f\u88c5\u306f
Array.prototype.map<\/code> \u3084
Array.prototype.filter<\/code> \u306e\u5b9f\u88c5\u3092\u7c21\u7565\u5316\u3057\u305f\u3082\u306e\u3068\u3057\u3066\u3044\u307e\u3059.<\/p>\n
GeneratorFunction<\/code> \u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3068\u306a\u3063\u3066\u3044\u307e\u3059. \u3053\u306e\u305f\u3081
GeneratorFunction<\/code> \u306e
prototype<\/code> \u306b
map<\/code> \u3084
filter<\/code> \u3092\u5b9f\u88c5\u3059\u308c\u3070\u826f\u3044\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3059.<\/p>\n
GeneratorFunction<\/code> \u306f global name \u3092\u6301\u305f\u306a\u3044\u305f\u3081, \u3053\u308c\u306e
prototype<\/code> \u3092\u76f4\u63a5\u7de8\u96c6\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093. \u305d\u3053\u30674\u884c\u76ee\u306e\u3088\u3046\u306b\u7a7a\u306e\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u3092\u4f5c\u6210\u3057\u3066\u305d\u306e
constructor<\/code> \u3092\u53d6\u5f97\u3057\u3066,
GeneratorFunction<\/code> \u3078\u306e\u53c2\u7167\u3092\u5f97\u3066\u3044\u307e\u3059.<\/p>\n
\u53c2\u8003<\/h2>\n
\n
\u66f4\u65b0<\/h2>\n
\n