views:

295

answers:

2

I'm porting some code I prototyped in python over to flash and while actionscript doesn't suck quite as bad as I expected (I hear v3 is quite a lot better than v2!) there's still some stuff I'm having to do that seems overly prosaic / boilerplate e.g. summing a list...

var a:int = 0;

for each ( var value:int in annual_saving )

    {

        a  = a + value;

    }

return a / 100;

as opposed to...

return reduce(lambda x,y: (x+y), self.annual_saving ) / 100

That feels a bit too much like Java for me (eww Java: puke! X-O###)

Am I simply ignorant of as3's cool array summing function? Or does it understand lambda calculus, or do list comprehensions? or provide some other such concise notation? Am I right in suspecting there is a more elegant way of doing this or am I stuck in the 20th century for the remainder of this project!?

Cheers :)

Roger.

+2  A: 

It doesn't do list comprehensions, but supports anonymous functions and closures. You also have map and filter in the Array class.

Vinay Sajip
+4  A: 

Actionscript is very similar to JS. You could easily implement it yourself if you had to:

var annual_saving = [50, 100, 50, 100];
function reduce (f, arr) {
    var a = arr[0];
    for (var i = 1; i < arr.length; i++) {
        a = f(a,arr[i]);
    }
    return a;
}
var res = reduce(function (x,y) { return x+y }, annual_saving);

You could easily extend this... the syntax will be somewhat less appealing, but still very concise.

jsight