ActionScript3.0 library of NiwaScript language engine, is licensed under the New BSD License.
niwan:
/
r = (\
Math.floor(Math.random() * @0));
randColor = (\ r(255) << 16 | r(255) << 8 | r(255));
f = (\
g.lineStyle(2, randColor());
g.beginFill(randColor());
g.drawRect(@0 * 30, @1 * 30, 30, 30);
g.endFill());
100.times(\
f(r(20), r(10)))
equivalent javascript:
r = function(n) {
return Math.floor(Math.random() * n);
};
randColor = function() {
return r(255) << 16 | r(255) << 8 | r (255);
};
f = function(x, y) {
g.lineStyle(2, randColor());
g.beginFill(randColor());
g.drawRect(x * 30, y * 30, 30, 30);
g.endFill();
};
i = 0;
while(i < 100) {
f(r(20), r(10));
i ++;
}
niwan:prototype programming
/
Array.def(lazy:true, fold,
a := self;
i := (@i == null).alt(0, @i);
(i >= self.size).alt(@0,
a.fold(i: i + 1, @1($1: @0, $2: a[i]), @1)));
[1,2,3,4,5,6,7,8,9,10].fold(0, \ $1 + $2 * $2)
equivalent javascript:
Array.prototype.fold = function(acc, f, i) {
i |= 0;
if(i >= this.length) {
return acc;
}
else {
return this.fold(f(acc, this[i]), f, i + 1);
}
};
[1,2,3,4,5,6,7,8,9,10].fold(0, function(acc, x) { return acc + x;});
niwan:meta programming(macro programming),you can also perform operation on the abstract syntax trees of the parameters
/
def(for(initial,condition,after,body), lazy: true,
initial.eval();
while_kari(condition.eval(),
body.eval();
after.eval()));
for(i := 0, i < 10, i ++,
log(i));
equivalent javascript:not so equivalent
my_for = function(initial, condition, after, body) {
eval(initial);
while(eval(condition)) {
eval(body);
eval(after);
}
}
my_for("i = 0", "i < 10", "i ++",
"console.log(i)");