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

add function maxVal & minVal #193

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

arcman7
Copy link

@arcman7 arcman7 commented Apr 17, 2015

The maxVal function will iterate through each of the properties in an object and select for the property that has the greatest numeric value. If there are no properties within the object that contain a numeric value the generic result ' ["key",-Infinity] ' is returned. The function minVal operates in the same manner except that it selects for the property with the least numeric value and it's default return value is ' ["key", Infinity] '.

This offers the user much shorter syntax than than ' _.max(obj,function(property){ return property; }); ' and also provides the key where the max value resides. I also included it's complimentary minVal function as well.

These functions provide a direct cross-over for ruby users looking for ' hash.max_by{|k,v| v} ' & ' hash.min_by{|k,v| v} '. Since these functions are specific to objects, I thought it would be appropriate to put this in object selectors.

The maxVal function will iterate through each of the properties in an object and select for the property that has the greatest numeric value. If there are no properties with in the object that contain a numeric value the generic result ' ["key",-Infinity] ' is returned. The function minVal operates in the same manner except that it selects for the property with the least numeric value and it's default return value is ' ["key", Infinity] '.

This offers the user much shorter syntax than than ' _.max(obj,function(property){property;}); ' and also provides the key where the max value resides. I also included it's complimentary minVal function as well. 

These functions provide a direct cross-over for ruby users looking for ' hash.max_by{|k,v| v} ' & ' hash.min_by{|k,v| v} '. Since these functions are specific to objects, I thought it would be appropriate to put this in object selectors.
@bjmiller
Copy link

You should be checking whether the values are numbers, or at least whether comparisons among them make sense.

1 < 'a' // => false
1 > 'a' // => false
1 === 'a' // => false

You might also want to look at the way core Underscore handles passing in comparators as "iteratees" in _.sortBy/_.max/_.min, and follow along with that. This way, people can determine their own way of figuring out which member of a mixed object what qualifies as the "maximum" and "minimum" value.

@arcman7
Copy link
Author

arcman7 commented Apr 17, 2015

Hey thank you for taking a look, I did have a look at the ._min and ._max but I was just thinking it might be nice to have short hand way to do that for objects. But I should generalize it to sort by some function. Also I did test it on objects containing strings and functions as well as numbers and it works correctly for me, is that not the case in general? Sorry I'm a bit new to coding, but I thought this might be a nice shortcut.

@jgonggrijp
Copy link
Contributor

Slapping an "after modules" onto this because it seems like it might require some more thought and also, there is not much code so it shouldn't be too much effort to fix some merge conflicts when continuing this branch.

@jgonggrijp jgonggrijp added after modules This should be postponed until after modularization (temporary label, see #220) question labels Aug 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
after modules This should be postponed until after modularization (temporary label, see #220) question
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants