"Cleaned up"? _.range (or maybe not)

This commit is contained in:
Ryan W Tenney
2010-10-05 22:36:15 -04:00
parent 81ce3ee0bd
commit 1c6e01173a

View File

@@ -353,16 +353,19 @@
// the native Python `range()` function. See // the native Python `range()` function. See
// [the Python documentation](http://docs.python.org/library/functions.html#range). // [the Python documentation](http://docs.python.org/library/functions.html#range).
_.range = function(start, stop, step) { _.range = function(start, stop, step) {
var a = slice.call(arguments); var args = slice.call(arguments),
var solo = a.length <= 1; solo = args.length <= 1,
var start = solo ? 0 : a[0], stop = solo ? a[0] : a[1], step = a[2] || 1; start = solo ? 0 : args[0],
var len = Math.ceil((stop - start) / step); stop = solo ? args[0] : args[1],
if (len <= 0) return []; step = args[2] || 1,
var range = new Array(len); len = Math.max(Math.ceil((stop - start) / step), 0),
for (var i = start, idx = 0; true; i += step) { idx = 0,
if ((step > 0 ? i - stop : stop - i) >= 0) return range; range = new Array(len);
range[idx++] = i; while (idx < len) {
range[idx++] = start;
start += step;
} }
return range;
}; };
// Function Functions // Function Functions