Actionscript3 是否提供列表推导式或 lambda 演算?

发布于 2024-08-03 05:33:40 字数 549 浏览 2 评论 0原文

我正在将我在 python 中原型化的一些代码移植到 flash,虽然 actionscript 并不像我预期的那么糟糕(我听说 v3 比 v2 好很多!),但我仍然需要做一些事情看起来过于平淡/样板,例如对列表进行求和...

var a:int = 0;

for each ( var value:int in annual_saving )

    {

        a  = a + value;

    }

return a / 100;

而不是...

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

这对我来说有点太像Java了(eww Java:puke!XO###)

我只是不知道as3的酷数组求和功能吗?或者它是否理解 lambda 演算,或者列表推导式?或者提供其他一些这样简洁的符号?我是否怀疑有一种更优雅的方法可以做到这一点,或者我在这个项目的其余部分中停留在 20 世纪!?

干杯:)

罗杰。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薆情海 2024-08-10 05:33:40

Actionscript 与 JS 非常相似。如果必须的话,您可以轻松地自己实现它:

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);

您可以轻松地扩展它...语法会稍微不那么吸引人,但仍然非常简洁。

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.

夏有森光若流苏 2024-08-10 05:33:40

它不执行列表推导式,但支持匿名函数和闭包。您还有 地图filter 在 Array 类中。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文