-
Notifications
You must be signed in to change notification settings - Fork 275
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(maxBy): Add maxBy function that select element that have max value by given condition in array #64
Conversation
@l2hyunwoo is attempting to deploy a commit to the Toss Team on Vercel. A member of the Team first needs to authorize it. |
src/math/maxBy.ts
Outdated
* const result = maxBy(people, person => person.age); | ||
* // result will be { name: 'Nunu', age: 30 } | ||
*/ | ||
export function maxBy<T>(elements: T[], selector: (element: T) => number): T | undefined { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export function maxBy<T>(elements: T[], selector: (element: T) => number): T | undefined { | |
export function maxBy<T>(elements: T[], selector: (element: T) => number): T { |
I believe returning undefined
is stricter and less error-prone.
However, following TypeScript's convention that arr[0]
is not nullable even if arr
is empty (unless noUncheckedIndexedAccess is on) is also important for consistency -- that's why we chose sample to not return nullable even if it can return undefined
.
For the sake of consistency, I believe we should not return undefined
here.
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
However, following TypeScript's convention that arr[0] is not nullable even if arr is empty
I tested this on TS Playground. And the result is below.
const array: number[] = [];
console.log(array[0]);
// [LOG]: undefined
I think this log says it's null(or undefined) when point first element of empty array.
I think usage without undefined makes compilcated usecase. Let's see below example code.
When we use undefined
const maxElement = maxBy<number>([]) ?? 0;
When we use Exception
let maxElement;
try {
maxElement = maxBy<number>([]);
} catch (error) {
// TODO: Logic when error occurred
}
const result = maxElement;
I think above one is more simple. What do you think?
But for the sake of consistency in this library, I also think it should not return undefined. Thanks for your advice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I thought dealing with empty arrays is what users of es-toolkit
have to handle in runtime...
(We do not check that the array is not empty using types)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see... It seems that usual convention in typescript. It's so hard to admit, but now I'm understand what you wrote. I'll change to non-nil type! Thanks for check it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After modification, all test cases pass well 👍🏻
src/math/maxBy.ts
Outdated
if (elements.length === 0) { | ||
return undefined; | ||
} | ||
let maxElement = elements[0]; | ||
let max = selector(maxElement); | ||
|
||
for (const element of elements) { | ||
const value = selector(element); | ||
|
||
if (value > max) { | ||
max = value; | ||
maxElement = element; | ||
} | ||
} | ||
|
||
return maxElement; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (elements.length === 0) { | |
return undefined; | |
} | |
let maxElement = elements[0]; | |
let max = selector(maxElement); | |
for (const element of elements) { | |
const value = selector(element); | |
if (value > max) { | |
max = value; | |
maxElement = element; | |
} | |
} | |
return maxElement; | |
let maxElement = elements[0]; | |
let max = -Infinity; | |
for (const element of elements) { | |
const value = selector(element); | |
if (value > max) { | |
max = value; | |
maxElement = element; | |
} | |
} | |
return maxElement; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's more readable I think. I'll fix it to this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It changed on this commit
Thanks, @l2hyunwoo ! |
close #55
I added a function that select element that have max value by given condition in array.