
{"id":2186,"date":"2016-12-30T22:16:31","date_gmt":"2016-12-31T03:16:31","guid":{"rendered":"http:\/\/www.ikriv.com\/blog\/?p=2186"},"modified":"2016-12-30T22:16:31","modified_gmt":"2016-12-31T03:16:31","slug":"javascript-wtf-5-no-map-for-iterables","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=2186","title":{"rendered":"JavaScript WTF #5: no map() for iterables"},"content":{"rendered":"<p>In the last few days I did some formal reading and informal experimenting with EcmaScript6, and compiled the list of its most annoying &#8220;features&#8221;.<\/p>\n<p><b>The fifth place goes to complete lack of standard functions for iterables<\/b>. EcmaScript 6 introduces a lot of cool stuff, but it brings a fair share of new WTFs, and adds new thrill to some existing ones. ES6 defines the concept of <i>iterable<\/i> and the for..of loop. You can now write something like this:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/\/ iterable\r\nfunction *range(start, len) {\r\n    for (var i=start; i&lt;start+len; ++i) yield i;\r\n}\r\n\r\nfor (var n of range(3,4)) console.log(n); \/\/ prints 3 4 5 6 \r\n<\/pre>\n<p>Most languages that define iterables (a.k.a enumerables, sequences, comprehensions) come with standard methods that operate on them, such as <code>map()<\/code>, <code>filter()<\/code>, or <code>groupBy()<\/code>, but JavaScript has none. It does have <code>map()<\/code>, <code>filter()<\/code>, <code>reduce()<\/code>, but only for arrays:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar arr = Array.from(range(1,10));\r\nconsole.log(arr);             \/\/ &#x5B; 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]\r\nconsole.log(arr.map(n=&gt;n*n)); \/\/ &#x5B; 1, 4, 9, 16, 25, 36, 49, 64, 81, 100 ]\r\n\r\nconsole.log(...range(1,10));  \/\/ 1 2 3 4 5 6 7 8 9 10\r\nconsole.log(...range(1,10).map(n=&gt;n*n)); \/\/ error\r\n<\/pre>\n<p>Of course, one can define necessary functions with relative ease, but their omission from the standard is very annoying. Most articles on iterables simply ignore the subject. I found a library called <a href=\"https:\/\/fitzgen.github.io\/wu.js\/\">wu.js<\/a> that claims to implement them, but their ES6 download link is broken. There is also a library called <a href=\"https:\/\/github.com\/kevinbarabash\/functify\">functify<\/a>. I also found comments from several people that said they tried to write a similar library and it was fun, but the result was horribly inefficient. If this is indeed the case, it makes iterables virtually useless.<\/p>\n<p><a href=\"https:\/\/www.codeproject.com\" rel=\"tag\" style=\"display:none\">CodeProject<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the last few days I did some formal reading and informal experimenting with EcmaScript6, and compiled the list of its most annoying &#8220;features&#8221;. The fifth place goes to complete <a href=\"https:\/\/ikriv.com\/blog\/?p=2186\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[4,15],"tags":[],"class_list":["entry","author-ikriv","post-2186","post","type-post","status-publish","format-standard","category-hack","category-webdev"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2186"}],"version-history":[{"count":15,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2186\/revisions"}],"predecessor-version":[{"id":2201,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2186\/revisions\/2201"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}