List-at-a-time functions in AS3

eokyere posted a pretty cool article/tips on how to make some of your AS3 code elegant in the case of applying functions to entire lists.
/*
For example, we have a list of donuts, and we want
to see which ones have chocolate, and then later
decide to take chunks of bites out of them:
*/

for (var i:Number = 0, n:Number = donuts.length; i < n; ++i)
if ((donuts[i] as Donut).hasChocolate())
v.push(donut[i]);

OR you can use Array’s filter function…

function choco(donunt:Donut):Boolean { return donut.hasChocolate(); }
var chocs:Array = donuts.filter(choco);

Very helpful read. It’s very easy to take for granted arrays and loops.

Link to eokyere’s blog entry on “Higher Order Actionscript” here.

Leave a Reply