{"id":335,"date":"2009-07-04T09:47:03","date_gmt":"2009-07-04T16:47:03","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1696"},"modified":"2009-07-04T09:47:03","modified_gmt":"2009-07-04T16:47:03","slug":"counter-function","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=335","title":{"rendered":"Counter Function"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar counter:Function = function(count:Array):Function{\r\n\tvar leng:int = count.length;\r\n\tvar index:int = 0;\r\n\treturn counter = function(reset:*=&quot;&quot;):Function{\r\n\t\tif (reset==&quot;reset&quot;){\r\n\t\t\tindex = 0;\r\n\t\t\treturn counter;\r\n\t\t}else\r\n\t\tif (reset is Array){\r\n\t\t\tcount = reset;\r\n\t\t\treturn counter;\r\n\t\t}\r\n\t\ttrace(count&#x5B;index % leng]);\r\n\t\tindex++;\r\n\t\treturn counter;\r\n\t}\r\n}\r\n\r\n\/\/ first time it&#039;s called you pass an array\r\ntrace(&quot;count through the array:&quot;);\r\n\/\/ you can optionally call trigger the counter by calling the newly\r\n\/\/ returned function\r\ncounter(&#x5B;&quot;a&quot;,&quot;b&quot;,&quot;c&quot;])();\r\ncounter();\r\ncounter();\r\ntrace(&quot;change the array:&quot;);\r\n\/\/ here we choose to simply set the array and not trigger the counter\r\ncounter(&#x5B;10,20,40,60,80]);\r\ncounter();\r\ncounter()\r\ntrace(&quot;reset the counter:&quot;);\r\n\/\/ reset and the counter is called 3 times\r\ncounter(&quot;reset&quot;)()()();\r\n\r\n\/*outputs :\r\ncount through the array:\r\na\r\nb\r\nc\r\nchange the array:\r\n10\r\n20\r\nreset the counter:\r\n10\r\n20\r\n40\r\n*\/\r\n<\/pre>\n<p>Today I felt like messing around with functions and this is the result. This snippet shows a strange and interesting technique to create a function that iterates through a given array in a few different ways. <\/p>\n<p>I believe the technical term for this kind of thing is a <strong>continuation<\/strong>&#8230;<\/p>\n<p>There are a bunch of other posts like this on actionsnippet. Some are purely experimental, others are actually quite useful&#8230; these can all be found in the <a href=\"https:\/\/actionsnippet.com\/?cat=3\">functions category<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>var counter:Function = function(count:Array):Function{ var leng:int = count.length; var index:int = 0; return counter = function(reset:*=&quot;&quot;):Function{ if (reset==&quot;reset&quot;){ index = 0; return counter; }else if (reset is Array){ count = reset; return counter; } trace(count&#x5B;index % leng]); index++; return counter; } } \/\/ first time it&#039;s called you pass an array trace(&quot;count through the array:&quot;); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,17],"tags":[3,14,4],"class_list":["post-335","post","type-post","status-publish","format-standard","hentry","category-dynamic","category-functions","tag-actionscript","tag-as3","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/335","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=335"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/335\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}