{"id":493,"date":"2010-03-12T19:23:12","date_gmt":"2010-03-13T02:23:12","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=2829"},"modified":"2010-03-12T19:23:12","modified_gmt":"2010-03-13T02:23:12","slug":"haskell-inspired-zipwith-function","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=493","title":{"rendered":"Haskell Inspired zipWith() Function"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\ninitOperators();\r\n\r\ntrace(zipWith(&quot;-&quot;, &#x5B;1,2,3], &#x5B;1,2,3]));\r\ntrace(zipWith(&quot;+&quot;, &#x5B;1,2,3], &#x5B;1,2,3]));\r\ntrace(zipWith(&quot;*&quot;, &#x5B;1,2,3], &#x5B;1,2,3]));\r\ntrace(zipWith(&quot;+&quot;, &#x5B;1,1,1,3], &#x5B;4,5,6,7]));\r\ntrace(zipWith(&quot;&lt;&lt;&quot;, &#x5B;2, 4], &#x5B;1,1]));\r\n\/*\r\noutputs:\r\n\r\n0,0,0\r\n2,4,6\r\n1,4,9\r\n5,6,7,10\r\n4,8\r\n*\/\r\n\r\nfunction zipWith(op:String, a:Array, b:Array):Array{\r\n    var aLeng:int = a.length;\r\n    var bLeng:int = b.length;\r\n    var leng:Number = (aLeng &lt; bLeng) ? aLeng : bLeng;\r\n    var zipped:Array = &#x5B;];\r\n   \r\n    if (!this&#x5B;op])return &#x5B;];\r\n   \r\n    for (var i:int = 0; i&lt;leng; i++){\r\n        zipped&#x5B;i]=this&#x5B;op](a&#x5B;i], b&#x5B;i]);\r\n    }\r\n    return zipped;\r\n}\r\n\r\nfunction initOperators():void{\r\n    this&#x5B;&quot;+&quot;]=function(a:Number, b:Number):Number{ return a + b };\r\n    this&#x5B;&quot;-&quot;]=function(a:Number, b:Number):Number{ return a - b };\r\n    this&#x5B;&quot;\/&quot;]=function(a:Number, b:Number):Number{ return a \/ b };\r\n    this&#x5B;&quot;*&quot;]=function(a:Number, b:Number):Number{ return a * b };\r\n    this&#x5B;&quot;%&quot;]=function(a:Number, b:Number):Number{ return a % b };\r\n   \r\n    this&#x5B;&quot;&amp;&quot;]=function(a:Number, b:Number):Number{ return a &amp; b };\r\n    this&#x5B;&quot;&lt;&lt;&quot;]=function(a:Number, b:Number):Number{ return a &lt;&lt; b };\r\n    this&#x5B;&quot;|&quot;]=function(a:Number, b:Number):Number{ return a | b };\r\n    this&#x5B;&quot;&gt;&gt;&quot;]=function(a:Number, b:Number):Number{ return a &gt;&gt; b };\r\n    this&#x5B;&quot;&gt;&gt;&gt;&quot;]=function(a:Number, b:Number):Number{ return a &gt;&gt;&gt; b };\r\n    this&#x5B;&quot;^&quot;]=function(a:Number, b:Number):Number{ return a ^ b };\r\n}\r\n<\/pre>\n<p>This snippet is basically like the haskell zipWith() function. It can combines two arrays into one array given a single function. In this case I defined a bunch of operator functions, but it would work with any kind of function that takes two arguments and returns a value. You could extend this to work with strings and do other strange things I guess. <\/p>\n<p>If you have yet to go play with haskell &#8230;<a href=\"http:\/\/haskell.org\/ghc\/download_ghc_6_12_1.html\"> go do it now<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>initOperators(); trace(zipWith(&quot;-&quot;, &#x5B;1,2,3], &#x5B;1,2,3])); trace(zipWith(&quot;+&quot;, &#x5B;1,2,3], &#x5B;1,2,3])); trace(zipWith(&quot;*&quot;, &#x5B;1,2,3], &#x5B;1,2,3])); trace(zipWith(&quot;+&quot;, &#x5B;1,1,1,3], &#x5B;4,5,6,7])); trace(zipWith(&quot;&lt;&lt;&quot;, &#x5B;2, 4], &#x5B;1,1])); \/* outputs: 0,0,0 2,4,6 1,4,9 5,6,7,10 4,8 *\/ function zipWith(op:String, a:Array, b:Array):Array{ var aLeng:int = a.length; var bLeng:int = b.length; var leng:Number = (aLeng &lt; bLeng) ? aLeng : bLeng; var zipped:Array = &#x5B;]; if (!this&#x5B;op])return &#x5B;]; for [&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,137,17,94,41,36,18],"tags":[3,14,4,154],"class_list":["post-493","post","type-post","status-publish","format-standard","hentry","category-arrays","category-binary","category-functions","category-misc","category-number","category-operators","category-return-values","tag-actionscript","tag-as3","tag-flash","tag-haskell"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/493","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=493"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/493\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}