-
Notifications
You must be signed in to change notification settings - Fork 9
feat: Add findInRange and findInRangeBinarySearch functions to Int and Nat.
#381
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
Open
Andrei1998
wants to merge
6
commits into
dfinity:main
Choose a base branch
from
Andrei1998:Andrei/binary-search
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fd5d94a
Functions for Nat.mo
Andrei1998 e3ff6c8
Functions for Int.mo
Andrei1998 2c9f78e
Add findInRange functions to Int and Nat.
Andrei1998 f07e596
Fix api.lock.json.
Andrei1998 631f577
Fix Changelog.md
Andrei1998 988ce82
Fix docs.
Andrei1998 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
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.
Implementation LGTM. I wonder if it might be difficult for developers / LLMs to understand when/how to correctly use this function. Maybe renaming to
findInRangeSorted()could help clarify?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 do have similar feelings on the naming here (somehow, no matter how you call it, it won't be completely compelling). Find in range sorted would make sense when calling it on some array/list etc., but would seem a bit off when using it to binary search on an abstract predicate like when computing a square root with binary search (ranges are already sorted, so the sorted here would refer to the predicate being "sorted"). Perhaps the most theoretical name would be findInRangeMonotonePredicate or findInRangeMonotone. The latter actually doesn't sound that bad (it's basically your suggestion but abstracting away the sortedness into monotonicity, which is what the method ultimately needs). Wdyt?
Leaving the naming aside, I think it's actually a good idea to make the docstring more "useful" (i.e., with standard use-cases like binary searching on an array and binary searching for the square root). Would that perhaps also help? This could still have a downside: if we later merge a PR like yours introducing collection-specific binary-search methods (which would be nice to have because of inlining and removing some bloat from the interface for those specific use-cases), users might be tempted to use the method in the enhanced docstring rather than looking up the collection-specific method.
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.
Maybe we could merge #375 first, since we would include those functions either way. I like the idea of showing more real-world-like examples in the doc comments.
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.
That works from my side. I am wondering whether we then want to also make the functions there do "find first" (C++-like) instead of "return position where it would be inserted" (Rust-like). It's true though that you will be able to do "find first" with these Int/Nat functions anyway, so maybe the diversity/inconsistency is actually good.