{"id":453,"date":"2009-12-31T05:54:49","date_gmt":"2009-12-31T12:54:49","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=2538"},"modified":"2009-12-31T05:54:49","modified_gmt":"2009-12-31T12:54:49","slug":"zip-function-for-arrays","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=453","title":{"rendered":"zip Function for Arrays"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar one:Array = &#x5B;1,2,3];\r\nvar two:Array = &#x5B;10, 20, 30];\r\n\r\nvar zipOneTwo:Array = zip(one, two);\r\n\r\n\/\/ trace each tupple\r\nfor each (var tuple:Array in zipOneTwo){\r\n\ttrace(tuple);\r\n}\r\n\r\n\/* outputs:\r\n1,10\r\n2,20\r\n3,30\r\n*\/\r\n\r\nfunction zip(a:Array, b:Array):Array{\r\n\tvar longest:Array = (a.length &gt;= b.length) ? a : b;\r\n\tvar zipped:Array = &#x5B;];\r\n\tfor (var i:int = 0; i&lt;longest.length; i++){\r\n\t\tzipped.push(&#x5B;a&#x5B;i], b&#x5B;i]]);\r\n\t}\r\n\treturn zipped;\r\n}\r\n<\/pre>\n<p>This snippet shows a function called zip that takes two arrays and returns a two dimensional array of tuples. Just imagine that each array is one side of a zipper and you&#8217;ll sort of get the idea&#8230;<\/p>\n<p>I do wish flash would trace this:<\/p>\n<p><code>[[1, 10], [2, 20], [3, 30]]<\/code><\/p>\n<p>We shouldn&#8217;t have to write a utility function to see the real array structure&#8230; <\/p>\n<p>I&#8217;ve been messing with <a href=\"http:\/\/www.haskell.org\/\" target=\"blank\">haskell<\/a> for a few days now&#8230; just for fun I thought I&#8217;d write a few functions inspired by it&#8230; this is the first one&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>var one:Array = &#x5B;1,2,3]; var two:Array = &#x5B;10, 20, 30]; var zipOneTwo:Array = zip(one, two); \/\/ trace each tupple for each (var tuple:Array in zipOneTwo){ trace(tuple); } \/* outputs: 1,10 2,20 3,30 *\/ function zip(a:Array, b:Array):Array{ var longest:Array = (a.length &gt;= b.length) ? a : b; var zipped:Array = &#x5B;]; for (var i:int = 0; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[3,14,4],"class_list":["post-453","post","type-post","status-publish","format-standard","hentry","category-arrays","tag-actionscript","tag-as3","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=453"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/453\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}