Repository navigation
feat(js/ts/python/dart/beam): support System.Array.BinarySearch and ResizeArray.BinarySearch - #5172
Merged
MangelMaxime merged 6 commits intoOct 27, 2026
Conversation
…Search Map the System.Array and ResizeArray BinarySearch overloads to new Array.binarySearch, binarySearchRange and binarySearchRangeCount library functions. The search follows .NET's probe order, the range overloads raise ArgumentOutOfRangeException and ArgumentException like .NET, and a missing or null comparer becomes the default comparer that ResizeArray.Sort() uses.
…ySearch Both use .NET's search loop, so arrays with duplicates and comparers that record their calls give .NET's answers. The range overloads check their arguments like .NET, raising ArgumentOutOfRangeException for a negative index or length (count for ResizeArray) and ArgumentException for a range past the end. The search lives in resize_array.py and only uses len() and indexing, so it serves Python lists and the Rust-backed arrays alike. An omitted or null comparer is the default comparer Fable passes to Array.sortInPlace.
…earch Adds binarySearch and binarySearchRange to the Dart Array library, following .NET's probe order so arrays with duplicates and custom comparers give the same results. The range overloads raise ArgumentOutOfRangeException and ArgumentException like .NET, with binarySearchRangeCount naming the count "count" for ResizeArray.BinarySearch(index, count, item, comparer). Overloads without a comparer, or with a null literal, use the default comparer that ResizeArray.Sort() uses.
…earch Both go to new fable_resize_array functions (binary_search, binary_search_range and binary_search_range_count), which search the list as a tuple in .NET's probe order after .NET's range checks. A missing or literal null comparer becomes the comparer Fable uses for compare, so unions follow declaration order. The non-generic System.Array overloads stay unsupported.
Member
|
Thank you |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Adds
System.Array.BinarySearchandResizeArray.BinarySearchto JS/TS, Python, Dart and Beam (Rust already has them and is unchanged). One commit per target. It doesn't depend on #5153 or #5154.Overloads:
System.Array.BinarySearch(array, value),(array, value, comparer),(array, index, length, value)and(array, index, length, value, comparer);xs.BinarySearch(item),(item, comparer)and(index, count, item, comparer).Compare(element, value)with the element first, complement of the insertion point on a miss), so arrays with duplicates and comparers with side effects give .NET's results.countfor ResizeArray), then the range, raisingArgumentOutOfRangeExceptionorArgumentExceptionlike .NET.nullone, the search uses the comparer Fable already sorts with on that target. On Beam that's the oneArray.sortInPlaceuses, sinceResizeArray.Sort()there orders union cases by name.binarySearch,binarySearchRangeandbinarySearchRangeCountinArray.fs(JS/TS, Dart), and the same in snake_case inresize_array.py(Python) andfable_resize_array.erl(Beam). The Python ones live inresize_array.pybecause they only needlen()and indexing, so they serve lists and the Rust-backed arrays alike, and putting them inarray_.pywould create an import cycle withsystem.binarySearchRangeCountonly exists because .NET names ResizeArray's countcount, notlength.Differences from .NET: strings compare ordinally (
Comparer<string>.Defaultis culture-aware), a comparer that's onlynullat runtime isn't replaced by the default, and an exception from the comparer isn't wrapped inInvalidOperationException(as with Fable's sorts). The non-genericSystem.Arrayoverloads reach the same code on JS/TS, Python and Dart but aren't tested; on Beam they stay unsupported.The tests use the same cases and .NET-checked values on every target. Dart and Beam use small comparer classes or object expressions, since
Comparer<int>.DefaultandComparisonIdentity.FromFunctionaren't supported there. The full CI passes on my fork: https://github.com/leonitousconforti/Fable/actions/runs/40390993903