{"id":4560,"date":"2025-01-15T22:27:17","date_gmt":"2025-01-15T13:27:17","guid":{"rendered":"https:\/\/www.wsmeguro.jp\/wp\/?p=4560"},"modified":"2025-01-15T22:35:10","modified_gmt":"2025-01-15T13:35:10","slug":"dart%e3%81%ae%e5%8b%89%e5%bc%b711%e2%88%923%ef%bc%9a%e5%90%8c%e6%9c%9f%e3%83%bb%e9%9d%9e%e5%90%8c%e6%9c%9f%ef%bc%88%e3%82%b8%e3%82%a7%e3%83%8d%e3%83%ac%e3%83%bc%e3%82%bf%e3%83%bc%ef%bc%9agenerator","status":"publish","type":"post","link":"https:\/\/www.wsmeguro.jp\/wp\/?p=4560","title":{"rendered":"Dart\u306e\u52c9\u5f3711\u22123\uff1a\u540c\u671f\u30fb\u975e\u540c\u671f\uff08\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u30fc\uff1aGenerator\uff09"},"content":{"rendered":"<p>\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u30fc\u306f\u3001\u5927\u91cf\u306e\u7e70\u308a\u8fd4\u3057\u51e6\u7406\u7b49\u3067\u4ee5\u4e0b\u306e\u7279\u5fb4\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<ul>\n<li>sync* \u306f\u540c\u671f\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u30fc\u95a2\u6570\u3092\u3001async*\u306f\u975e\u540c\u671f\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u30fc\u95a2\u6570\u3092\u793a\u3057\u307e\u3059\u3002<\/il>\n<li>yield \u30ad\u30fc\u30ef\u30fc\u30c9\u3092\u4f7f\u3063\u3066\u5024\u30921\u3064\u305a\u3064\u8fd4\u3057\u307e\u3059\u3002<\/li>\n<li>\u3059\u3079\u3066\u306e\u5024\u3092\u4e00\u5ea6\u306b\u8a08\u7b97\u3057\u306a\u3044\u305f\u3081\u3001\u5927\u304d\u306a\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3067\u3082\u52b9\u7387\u7684\u3067\u3059\u3002<\/li>\n<\/ul>\n<p>\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u30fc\u306b\u306f\u540c\u671f\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u30fc\u3068\u975e\u540c\u671f\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u30fc\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<ol>\n<li>\u540c\u671f\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u30fc<br \/>\n\u30ea\u30b9\u30c8\u3084\u30bb\u30c3\u30c8\u306a\u3069\u306e\u7e70\u308a\u8fd4\u3057\u53ef\u80fd\u306a\u30b3\u30ec\u30af\u30b7\u30e7\u30f3Iterable\u3092\u751f\u6210\u3057\u307e\u3059\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-dart\" data-lang=\"Dart\">\r\nIterable&lt;int&gt; syncGenerator(int n) sync* {\r\n  for (int i = 0; i < n; i++) {\r\n    yield i; \/\/ \u5024\u30921\u3064\u305a\u3064\u8fd4\u3059\r\n  }\r\n}\r\nvoid main() {\r\n  var nums = syncGenerator(5); \/\/ 0\u304b\u30894\u307e\u3067\u306e\u5024\u3092\u751f\u6210\r\n  for (var num in nums) {\r\n    print(num); \/\/ 0, 1, 2, 3, 4 \u3092\u9806\u306b\u8868\u793a\r\n  }\r\n}\r\n<\/pre>\n<\/div>\n<\/li>\n<li>\u975e\u540c\u671f\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u30fc<br \/>\n\u975e\u540c\u671f\u51e6\u7406\u3092\u4f34\u3046\u5024\u306e\u30b9\u30c8\u30ea\u30fc\u30e0\u3092\u751f\u6210\u3057\u307e\u3059\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-dart\" data-lang=\"Dart\">\r\nStream&lt;int&gt; asyncGenerator(int n) async* {\r\n  for (int i = 0; i < n; i++) {\r\n    await Future.delayed(Duration(seconds: 1)); \/\/ 1\u79d2\u5f85\u3064\r\n    yield i; \/\/ \u5024\u30921\u3064\u305a\u3064\u8fd4\u3059\r\n  }\r\n}\r\n\r\nvoid main() async {\r\n  var nums = asyncGenerator(5); \/\/ 0\u304b\u30894\u307e\u3067\u306e\u5024\u30921\u79d2\u3054\u3068\u306b\u751f\u6210\r\n  await for (var num in nums) {\r\n    print(num); \/\/ 1\u79d2\u3054\u3068\u306b 0, 1, 2, 3, 4 \u3092\u9806\u306b\u8868\u793a\r\n  }\r\n}\r\n<\/pre>\n<\/div>\n<p>\u30bb\u30f3\u30b5\u30fc\u304b\u3089\u306e\u30c7\u30fc\u30bf\u53d6\u5f97\u3084\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u304b\u3089\u306e\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u30c7\u30fc\u30bf\u53d6\u5f97\u306a\u3069\u3001\u975e\u540c\u671f\u306e\u30b7\u30ca\u30ea\u30aa\u3067\u4fbf\u5229\u3067\u3059\u3002<\/li>\n<li>\u5148\u751f\u306b\u3088\u308b\u307e\u3068\u3081<br \/>\n\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u30fc\u304c\u5927\u91cf\u306e\u753b\u50cf\u51e6\u7406\u3067\u5f79\u7acb\u3064\u7406\u7531<\/li>\n<p>\t(1)\t\u9045\u5ef6\u8a55\u4fa1\uff08lazy evaluation\uff09:<br \/>\n\t\u2022\t\u3059\u3079\u3066\u306e\u753b\u50cf\u3092\u4e00\u5ea6\u306b\u30e1\u30e2\u30ea\u306b\u8aad\u307f\u8fbc\u3080\u306e\u3067\u306f\u306a\u304f\u3001\u5fc5\u8981\u306a\u30bf\u30a4\u30df\u30f3\u30b0\u30671\u3064\u305a\u3064\u8aad\u307f\u8fbc\u307f\u30fb\u51e6\u7406\u3067\u304d\u307e\u3059\u3002<br \/>\n\t\u2022\t\u30e1\u30e2\u30ea\u3092\u52b9\u7387\u7684\u306b\u4f7f\u3048\u308b\u305f\u3081\u3001\u5927\u91cf\u306e\u30c7\u30fc\u30bf\u3092\u6271\u3046\u969b\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u304c\u5411\u4e0a\u3057\u307e\u3059\u3002<br \/>\n\t(2)\t\u30b9\u30c8\u30ea\u30fc\u30e0\u51e6\u7406:<br \/>\n\t\u2022\t\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u30fc\u306f\u6b21\u306e\u753b\u50cf\u3092\u751f\u6210\u3059\u308b\u307e\u3067\u5f85\u6a5f\u3059\u308b\u306e\u3067\u3001\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u51e6\u7406\u304c\u53ef\u80fd\u3067\u3059\u3002<br \/>\n\t\u2022\t\u305f\u3068\u3048\u3070\u3001\u753b\u50cf\u30921\u3064\u305a\u3064\u51e6\u7406\u3057\u3066\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3057\u306a\u304c\u3089\u3001\u6b21\u306e\u753b\u50cf\u3092\u51e6\u7406\u3067\u304d\u307e\u3059\u3002<br \/>\n\t(3)\t\u5927\u898f\u6a21\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306e\u30b9\u30b1\u30fc\u30e9\u30d3\u30ea\u30c6\u30a3:<br \/>\n\t\u2022\t1\u4e07\u679a\u300110\u4e07\u679a\u3068\u3044\u3063\u305f\u81a8\u5927\u306a\u753b\u50cf\u3067\u3082\u3001\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u30fc\u3092\u4f7f\u3048\u3070\u52b9\u7387\u3088\u304f\u51e6\u7406\u3067\u304d\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u30fc\u306f\u3001\u5927\u91cf\u306e\u7e70\u308a\u8fd4\u3057\u51e6\u7406\u7b49\u3067\u4ee5\u4e0b\u306e\u7279\u5fb4\u304c\u3042\u308a\u307e\u3059\u3002 sync* \u306f\u540c\u671f\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u30fc\u95a2\u6570\u3092\u3001async*\u306f\u975e\u540c\u671f\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u30fc\u95a2\u6570\u3092\u793a\u3057\u307e\u3059\u3002 yield \u30ad\u30fc\u30ef\u30fc\u30c9\u3092\u4f7f\u3063\u3066\u5024\u30921\u3064\u305a\u3064\u8fd4\u3057\u307e\u3059\u3002 \u3059\u3079\u3066\u306e\u5024&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"footnotes":""},"categories":[595,505],"tags":[601,619],"class_list":["post-4560","post","type-post","status-publish","format-standard","hentry","category-dart2","category-programming","tag-dart"],"_links":{"self":[{"href":"https:\/\/www.wsmeguro.jp\/wp\/index.php?rest_route=\/wp\/v2\/posts\/4560","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsmeguro.jp\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsmeguro.jp\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsmeguro.jp\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsmeguro.jp\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4560"}],"version-history":[{"count":7,"href":"https:\/\/www.wsmeguro.jp\/wp\/index.php?rest_route=\/wp\/v2\/posts\/4560\/revisions"}],"predecessor-version":[{"id":4567,"href":"https:\/\/www.wsmeguro.jp\/wp\/index.php?rest_route=\/wp\/v2\/posts\/4560\/revisions\/4567"}],"wp:attachment":[{"href":"https:\/\/www.wsmeguro.jp\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsmeguro.jp\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsmeguro.jp\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}