add async version of Ox.loop
This commit is contained in:
parent
11aa611a58
commit
b4dc9e15ba
1 changed files with 54 additions and 13 deletions
|
@ -230,20 +230,20 @@ Ox.loop <f> For-loop, functional-style
|
||||||
<code>break</code> statement. Unlike a <code>for</code> loop,
|
<code>break</code> statement. Unlike a <code>for</code> loop,
|
||||||
<code>Ox.loop</code> doesn't leak its counter variable to the outer scope,
|
<code>Ox.loop</code> doesn't leak its counter variable to the outer scope,
|
||||||
but returns it.
|
but returns it.
|
||||||
(stop, callback) -> <n> Next value
|
(stop, fn) -> <n> Next value
|
||||||
equivalent to <code>for (var i = 0; i < stop; i++)</code>
|
equivalent to <code>for (var i = 0; i < stop; i++) { fn(i); }</code>
|
||||||
(start, stop, callback) -> <n> Next value
|
(start, stop, fn) -> <n> Next value
|
||||||
equivalent to <code>for (var i = start; i < stop; i++)</code> or,
|
equivalent to <code>for (var i = start; i < stop; i++) { fn(i); }</code>
|
||||||
if <code>start</code> is larger than <code>stop</code>,
|
or, if <code>start</code> is larger than <code>stop</code>,
|
||||||
<code>for (var i = start; i > stop; i--)</code>
|
<code>for (var i = start; i > stop; i--) { fn(i); }</code>
|
||||||
(start, stop, step, callback) -> <n> Next value
|
(start, stop, step, fn) -> <n> Next value
|
||||||
equivalent to <code>for (var i = start; i < stop; i += step)</code> or,
|
equivalent to <code>for (var i = start; i < stop; i += step) { fn(i);
|
||||||
if <code>step</code> is negative,
|
}</code> or, if <code>step</code> is negative, <code>for (var i = start;
|
||||||
<code>for (var i = start; i > stop; i += step)</code>
|
i > stop; i += step) { fn(i); }</code>
|
||||||
start <n> Start value
|
start <n> Start value
|
||||||
stop <n> Stop value (exclusive)
|
stop <n> Stop value (exclusive)
|
||||||
step <n> Step value
|
step <n> Step value
|
||||||
callback <f> Iterator function
|
fn <f> Iterator function
|
||||||
i <n> Counter value
|
i <n> Counter value
|
||||||
> Ox.loop(10, function(i) { return i != 4; })
|
> Ox.loop(10, function(i) { return i != 4; })
|
||||||
4
|
4
|
||||||
|
@ -255,16 +255,57 @@ Ox.loop = function() {
|
||||||
start = len > 2 ? arguments[0] : 0,
|
start = len > 2 ? arguments[0] : 0,
|
||||||
stop = arguments[len > 2 ? 1 : 0],
|
stop = arguments[len > 2 ? 1 : 0],
|
||||||
step = len == 4 ? arguments[2] : (start <= stop ? 1 : -1),
|
step = len == 4 ? arguments[2] : (start <= stop ? 1 : -1),
|
||||||
callback = arguments[len - 1],
|
fn = arguments[len - 1],
|
||||||
i;
|
i;
|
||||||
for (i = start; step > 0 ? i < stop : i > stop; i += step) {
|
for (i = start; step > 0 ? i < stop : i > stop; i += step) {
|
||||||
if (callback(i) === false) {
|
if (fn(i) === false) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return i;
|
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
|
Ox.print <f> Prints its arguments to the console
|
||||||
(arg, ...) -> <s> String
|
(arg, ...) -> <s> String
|
||||||
|
|
Loading…
Reference in a new issue