Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine _.min and _.max #2142

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 20 additions & 43 deletions underscore.js
Expand Up @@ -277,56 +277,33 @@
return _.find(obj, _.matcher(attrs));
};

// Return the maximum element (or element-based computation).
_.max = function(obj, iteratee, context) {
var result = -Infinity, lastComputed = -Infinity,
value, computed;
if (iteratee == null && obj != null) {
obj = isArrayLike(obj) ? obj : _.values(obj);
for (var i = 0, length = obj.length; i < length; i++) {
value = obj[i];
if (value > result) {
result = value;
}
}
} else {
// Generator function to create the max and min functions
var createExtremumFinder = function(max) {
return function(obj, iteratee, context) {
var keys = !isArrayLike(obj) && _.keys(obj),
length = (keys || obj).length,
result = max ? -Infinity : Infinity,
lastComputed = result,
found = false;
iteratee = cb(iteratee, context);
_.each(obj, function(value, index, list) {
computed = iteratee(value, index, list);
if (computed > lastComputed || computed === -Infinity && result === -Infinity) {
for (var index = 0; index < length; index++) {
var currentKey = keys ? keys[index] : index;
var value = obj[currentKey];
var computed = iteratee(value, currentKey, obj);
if ((max ? computed > lastComputed : computed < lastComputed) || (!found && computed === result)) {
found = true;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the found variable necessary? Can't you just infer it based on index

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the iteratee can return anything. So, if the first iteration returns undefined, that won't be greater than -Infinity. But, the next iteration might be equal.

result = value;
lastComputed = computed;
}
});
}
return result;
};

// Return the minimum element (or element-based computation).
_.min = function(obj, iteratee, context) {
var result = Infinity, lastComputed = Infinity,
value, computed;
if (iteratee == null && obj != null) {
obj = isArrayLike(obj) ? obj : _.values(obj);
for (var i = 0, length = obj.length; i < length; i++) {
value = obj[i];
if (value < result) {
result = value;
}
}
} else {
iteratee = cb(iteratee, context);
_.each(obj, function(value, index, list) {
computed = iteratee(value, index, list);
if (computed < lastComputed || computed === Infinity && result === Infinity) {
result = value;
lastComputed = computed;
}
});
}
return result;
return result;
};
};

// Return the extremum element (or element-based computation).
_.max = createExtremumFinder(true);
_.min = createExtremumFinder(false);

// Shuffle a collection, using the modern version of the
// [Fisher-Yates shuffle](http://en.wikipedia.org/wiki/Fisher–Yates_shuffle).
_.shuffle = function(obj) {
Expand Down