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

feat(collections): add minWith #1169

Merged
merged 2 commits into from
Sep 2, 2021
Merged

feat(collections): add minWith #1169

merged 2 commits into from
Sep 2, 2021

Conversation

majidsajadi
Copy link
Contributor

This PR adds minWith to the collections module.

@LionC
Copy link
Contributor

LionC commented Aug 26, 2021

That is actually really nice - it is a lot more efficient than sorting, because it does not need a copy (which sort needs if you do not want to mutate) and it has a complexity of O(n), while sorting is O(n * log n).

I initially didn't realize that when making the initial proposal and glancing over those functions, but that makes a lot of sense. I will add a review tomorrow.

@LionC LionC mentioned this pull request Aug 27, 2021
44 tasks
@LionC
Copy link
Contributor

LionC commented Aug 27, 2021

See my new review on maxWith which applies here as well

Copy link
Member

@kt3k kt3k left a comment

Choose a reason for hiding this comment

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

@majidsajadi LGTM. Thanks!

@kt3k
Copy link
Member

kt3k commented Sep 2, 2021

Side note: This should be useful when the comparator is very complex (like the below) and can't be rewritten with minBy easily.

minWith(array, (a, b) => {
  if (a.foo !== b.foo) {
    return a.foo - b.foo;
  }

  if (a.bar !== b.bar) {
    return a.bar.length - b.bar.length;
  }

  return a.baz - b.baz;
});

@kt3k kt3k merged commit ca23a1e into denoland:main Sep 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants