Simplify lazy chaining.

This commit is contained in:
John-David Dalton
2014-10-30 20:54:04 -07:00
parent cdf5ac4fc4
commit cf724c43cb

View File

@@ -4799,12 +4799,17 @@
*/ */
function LazyWrapper(value) { function LazyWrapper(value) {
this.dir = 1; this.dir = 1;
this.dropCount = 0;
this.filtered = false; this.filtered = false;
this.iteratees = []; this.iteratees = [];
this.takeCount = POSITIVE_INFINITY;
this.views = [];
this.wrapped = value; this.wrapped = value;
this.end =
this.endAfter =
this.startRight = POSITIVE_INFINITY;
this.start =
this.startAfter =
this.endRight = 0;
} }
/** /**
@@ -4818,11 +4823,17 @@
function lazyClone() { function lazyClone() {
var result = new LazyWrapper(this.wrapped); var result = new LazyWrapper(this.wrapped);
result.dir = this.dir; result.dir = this.dir;
result.dropCount = this.dropCount;
result.filtered = this.filtered; result.filtered = this.filtered;
result.takeCount = this.takeCount;
result.end = this.end;
result.endAfter = this.endAfter;
result.endRight = this.endRight;
result.start = this.start;
result.startAfter = this.startAfter;
result.startRight = this.startRight;
push.apply(result.iteratees, this.iteratees); push.apply(result.iteratees, this.iteratees);
push.apply(result.views, this.views);
return result; return result;
} }
@@ -4854,28 +4865,15 @@
function lazyValue() { function lazyValue() {
var array = this.wrapped.value(), var array = this.wrapped.value(),
length = array.length, length = array.length,
start = 0, start = this.start,
end = length, end = length - this.endRight;
views = this.views,
viewIndex = -1,
viewsLength = views.length;
while (++viewIndex < viewsLength) { start = nativeMax(start, end - this.startRight);
var view = views[viewIndex], end = nativeMin(end, start + this.end);
size = view.size;
switch (view.type) {
case 'drop': start += size; break;
case 'dropRight': end -= size; break;
case 'take': end = nativeMin(end, start + size); break;
case 'takeRight': start = nativeMax(start, end - size); break;
}
}
var dir = this.dir, var dir = this.dir,
dropCount = this.dropCount, startAfter = this.startAfter,
droppedCount = 0, endAfter = nativeMin(end - start, this.endAfter - startAfter),
doneDropping = !dropCount,
takeCount = nativeMin(end - start, this.takeCount - dropCount),
isRight = dir < 0, isRight = dir < 0,
index = isRight ? end : start - 1, index = isRight ? end : start - 1,
iteratees = this.iteratees, iteratees = this.iteratees,
@@ -4884,9 +4882,11 @@
result = []; result = [];
outer: outer:
while (length-- && resIndex < takeCount) { while (length-- && resIndex < endAfter) {
index += dir;
var iterateesIndex = -1, var iterateesIndex = -1,
value = array[index += dir]; value = array[index];
while (++iterateesIndex < iterateesLength) { while (++iterateesIndex < iterateesLength) {
var data = iteratees[iterateesIndex], var data = iteratees[iterateesIndex],
@@ -4904,10 +4904,10 @@
} }
} }
} }
if (doneDropping) { if (startAfter) {
result[resIndex++] = value; startAfter--;
} else { } else {
doneDropping = ++droppedCount >= dropCount; result[resIndex++] = value;
} }
} }
return isRight ? result.reverse() : result; return isRight ? result.reverse() : result;
@@ -10019,22 +10019,19 @@
}); });
// add `LazyWrapper` methods for `_.drop` and `_.take` variants // add `LazyWrapper` methods for `_.drop` and `_.take` variants
arrayEach(['drop', 'take'], function(methodName) { arrayEach(['drop', 'take'], function(methodName, index) {
var countName = methodName + 'Count', var rangeName = index ? 'end' : 'start',
afterName = rangeName + 'After',
rightName = (index ? 'start' : 'end') + 'Right',
whileName = methodName + 'While'; whileName = methodName + 'While';
LazyWrapper.prototype[methodName] = function(n) { LazyWrapper.prototype[methodName] = function(n) {
n = n == null ? 1 : nativeMax(+n || 0, 0); var result = this.clone(),
key = result.filtered ? afterName : (result.dir < 0 ? rightName : rangeName),
value = result[key];
var result = this.clone(); n = n == null ? 1 : nativeMax(+n || 0, 0);
if (this.filtered) { result[key] = (nativeIsFinite(value) ? value : 0) + n;
result[countName] = n;
return result;
}
result.views.push({
'size': n,
'type': methodName + (result.dir < 0 ? 'Right' : '')
});
return result; return result;
}; };