add async version of Ox.loop

This commit is contained in:
rolux 2012-04-13 23:19:52 +02:00
parent 11aa611a58
commit b4dc9e15ba

View file

@ -230,20 +230,20 @@ Ox.loop <f> For-loop, functional-style
<code>break</code> statement. Unlike a <code>for</code> loop,
<code>Ox.loop</code> doesn't leak its counter variable to the outer scope,
but returns it.
(stop, callback) -> <n> Next value
equivalent to <code>for (var i = 0; i < stop; i++)</code>
(start, stop, callback) -> <n> Next value
equivalent to <code>for (var i = start; i < stop; i++)</code> or,
if <code>start</code> is larger than <code>stop</code>,
<code>for (var i = start; i > stop; i--)</code>
(start, stop, step, callback) -> <n> Next value
equivalent to <code>for (var i = start; i < stop; i += step)</code> or,
if <code>step</code> is negative,
<code>for (var i = start; i > stop; i += step)</code>
(stop, fn) -> <n> Next value
equivalent to <code>for (var i = 0; i < stop; i++) { fn(i); }</code>
(start, stop, fn) -> <n> Next value
equivalent to <code>for (var i = start; i < stop; i++) { fn(i); }</code>
or, if <code>start</code> is larger than <code>stop</code>,
<code>for (var i = start; i > stop; i--) { fn(i); }</code>
(start, stop, step, fn) -> <n> Next value
equivalent to <code>for (var i = start; i < stop; i += step) { fn(i);
}</code> or, if <code>step</code> is negative, <code>for (var i = start;
i > stop; i += step) { fn(i); }</code>
start <n> Start value
stop <n> Stop value (exclusive)
step <n> Step value
callback <f> Iterator function
fn <f> Iterator function
i <n> Counter value
> Ox.loop(10, function(i) { return i != 4; })
4
@ -255,16 +255,57 @@ Ox.loop = function() {
start = len > 2 ? arguments[0] : 0,
stop = arguments[len > 2 ? 1 : 0],
step = len == 4 ? arguments[2] : (start <= stop ? 1 : -1),
callback = arguments[len - 1],
fn = arguments[len - 1],
i;
for (i = start; step > 0 ? i < stop : i > stop; i += step) {
if (callback(i) === false) {
if (fn(i) === false) {
break;
}
}
return i;
};
Ox._loop = function() {
var type = Ox.toArray(arguments).map(function(arg) {
return Ox.typeOf(arg);
}),
fnIndex = type.indexOf('function'),
callbackIndex = type.lastIndexOf('function'),
nIndex = callbackIndex - 1,
hasStart = type[1] == 'number',
hasStep = hasStart && type[2] == 'number',
hasCallback = fnIndex != callbackIndex,
hasN = hasCallback && type[nIndex] == 'number',
start = hasStart ? arguments[0] : 0,
stop = arguments[hasStart ? 1 : 0],
step = hasStep ? arguments[2] : (start <= stop ? 1 : -1),
fn = arguments[fnIndex],
n = hasN ? arguments[nIndex] / step : 1,
callback = hasCallback ? arguments[callbackIndex] : null;
function loop(start_, stop_, callback_) {
var i, isFalse;
for (i = start_; step > 0 ? i < stop_ : i > stop_; i += step) {
if (isFalse = fn(i) === false) {
break;
}
}
callback_ && callback_(i, !isFalse);
return i;
}
function next(start_) {
setTimeout(function() {
loop(start_, Math.min(start_ + n, stop), function(i, ret) {
(ret && i < stop ? next : callback)(i);
});
});
}
if (hasCallback) {
next(start);
} else {
return loop(start, stop);
}
};
/*@
Ox.print <f> Prints its arguments to the console
(arg, ...) -> <s> String