Actionscript:
-
var counter:Function = function(count:Array):Function{
-
var leng:int = count.length;
-
var index:int = 0;
-
return counter = function(reset:*=""):Function{
-
if (reset=="reset"){
-
index = 0;
-
return counter;
-
}else
-
if (reset is Array){
-
count = reset;
-
return counter;
-
}
-
trace(count[index % leng]);
-
index++;
-
return counter;
-
}
-
}
-
-
// first time it's called you pass an array
-
trace("count through the array:");
-
// you can optionally call trigger the counter by calling the newly
-
// returned function
-
counter(["a","b","c"])();
-
counter();
-
counter();
-
trace("change the array:");
-
// here we choose to simply set the array and not trigger the counter
-
counter([10,20,40,60,80]);
-
counter();
-
counter()
-
trace("reset the counter:");
-
// reset and the counter is called 3 times
-
counter("reset")()()();
-
-
/*outputs :
-
count through the array:
-
a
-
b
-
c
-
change the array:
-
10
-
20
-
reset the counter:
-
10
-
20
-
40
-
*/
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.
I believe the technical term for this kind of thing is a continuation...
There are a bunch of other posts like this on actionsnippet. Some are purely experimental, others are actually quite useful... these can all be found in the functions category.