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
Converting binary search helper to TS #2129
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
type BinarySearchComparison < T > = (candidate: T) => -1 | 0 | 1; | ||
|
||
const BinarySearch = { | ||
|
||
/** | ||
* Searches for an item in an array which matches a certain condition. | ||
* This requires the condition to only match one item in the array, | ||
* and for the array to be ordered. | ||
* | ||
* @param {Array<T>} list The array to search. | ||
* @param {BinarySearchComparison<T>} comparisonFn | ||
* Called and provided a candidate item as the first argument. | ||
* Should return: | ||
* > -1 if the item should be located at a lower index than the provided item. | ||
* > 1 if the item should be located at a higher index than the provided item. | ||
* > 0 if the item is the item you're looking for. | ||
* | ||
* @return {T | null} The object if it is found or null otherwise. | ||
*/ | ||
search: function<T> (list: T[], comparisonFn: BinarySearchComparison<T>): T | null { | ||
let minIndex: number = 0; | ||
let maxIndex: number = list.length - 1; | ||
let currentIndex: number | null = null; | ||
let currentElement: T | null = null; | ||
|
||
while (minIndex <= maxIndex) { | ||
currentIndex = (minIndex + maxIndex) / 2 | 0; | ||
currentElement = list[currentIndex]; | ||
|
||
let comparisonResult = comparisonFn(currentElement); | ||
if (comparisonResult > 0) { | ||
minIndex = currentIndex + 1; | ||
} else if (comparisonResult < 0) { | ||
maxIndex = currentIndex - 1; | ||
} else { | ||
return currentElement; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
}; | ||
|
||
export default BinarySearch; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nit:
<T>
Also we should
export type
this. Generally anything that is used on the public path should be exportedThere 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 I remember correctly, changing
< T >
to<T>
resulted in this eslint error: https://eslint.org/docs/rules/space-infix-opsI'd hate to suppress that eslint rule though, as it should error/warn in the case of
const sum = 1+ 2
shouldn't 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.
Ah if it's a lint thing then fine as long as it is consistent everywhere :)