fjsc: add map and reduce back

chris.double 2006-12-17 00:09:19 +00:00
parent a1f1d42c2c
commit 40b9a43aef
1 changed files with 37 additions and 6 deletions

View File

@ -31,12 +31,14 @@ function Factor() {
var factor = new Factor();
Factor.prototype.call_next = function(next) {
if(this.nesting++ > 150) {
this.nesting = 0;
setTimeout(next, 0);
}
else {
next();
if(next) {
if(this.nesting++ > 150) {
this.nesting = 0;
setTimeout(next, 0);
}
else {
next();
}
}
}
@ -299,3 +301,32 @@ factor.words["alien-invoke"] = new Word("alien-invoke", "primitive", function(ne
stack.push(v);
factor.call_next(next);
});
factor.words["map"] = new Word("map", "primitive", function(next) {
var stack = factor.cont.data_stack;
var quot = stack.pop();
var seq = stack.pop();
var result = [ ];
for(var i=0;i<seq.length;++i) {
stack.push(seq[i]);
quot.execute();
result[i]=stack.pop();
}
stack.push(result);
factor.call_next(next);
});
factor.words["reduce"] = new Word("reduce", "primitive", function(next) {
var stack = factor.cont.data_stack;
var quot = stack.pop();
var prev = stack.pop();
var seq = stack.pop();
for(var i=0;i<seq.length;++i) {
stack.push(prev);
stack.push(seq[i]);
quot.execute();
prev=stack.pop();
}
stack.push(prev);
factor.call_next(next);
});