[Repo Assist] feat: add filter, iter, exists, forall, find, choose, head, toArray, toList, ofList to PersistentVector#247
Open
github-actions[bot] wants to merge 2 commits intomasterfrom
Conversation
…toList, ofList to PersistentVector Adds 14 new module-level functions to PersistentVector, further addressing issue #152 (aligning collection module functions with FSharp.Collections). New functions: - choose – filter and map in one pass - exists – short-circuiting 'any' check - filter – return elements satisfying predicate - find – first matching element (throws if not found) - tryFind – first matching element (returns option) - findIndex – index of first match (throws if not found) - tryFindIndex – index of first match (returns option) - forall – short-circuiting 'all' check - head – first element (throws if empty) - tryHead – first element (returns option) - iter – apply action to each element - iteri – apply action with index to each element - ofList – create from a list - toArray – convert to an array - toList – convert to a list Adds 26 new unit tests covering normal cases, empty vectors, and error paths. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
29 tasks
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a set of PersistentVector module functions to better align the collection API with FSharp.Collections (per #152), along with unit tests validating the new behavior.
Changes:
- Added new module-level functions to
PersistentVector(e.g.,filter,choose,exists,forall,find*,head/tryHead,iter/iteri,toArray/toList,ofList). - Updated
PersistentVectorpublic signatures in the.fsito expose the new APIs. - Added unit tests covering common and edge-case scenarios for the new functions.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| tests/FSharpx.Collections.Tests/PersistentVectorTest.fs | Adds unit tests for the newly introduced PersistentVector module functions. |
| src/FSharpx.Collections/PersistentVector.fsi | Exposes the new PersistentVector module functions in the public signature. |
| src/FSharpx.Collections/PersistentVector.fs | Implements the new PersistentVector module functions (filtering, searching, iteration, conversions). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
Comment on lines
+627
to
+640
| let mutable result = -1 | ||
| let mutable i = 0 | ||
|
|
||
| while result = -1 && i < vector.Length do | ||
| if f vector.[i] then | ||
| result <- i | ||
|
|
||
| i <- i + 1 | ||
|
|
||
| if result = -1 then | ||
| raise(System.Collections.Generic.KeyNotFoundException("An element satisfying the predicate was not found in the collection.")) | ||
| else | ||
| result | ||
|
|
| i <- i + 1 | ||
|
|
||
| if result = -1 then | ||
| raise(System.Collections.Generic.KeyNotFoundException("An element satisfying the predicate was not found in the collection.")) |
Comment on lines
+710
to
+716
| let toArray(vector: PersistentVector<'T>) = | ||
| let arr = Array.zeroCreate vector.Length | ||
| let mutable i = 0 | ||
|
|
||
| for item in vector do | ||
| arr.[i] <- item | ||
| i <- i + 1 |
Comment on lines
+533
to
+536
| test "findIndex returns index of first matching element" { | ||
| let v = PersistentVector.ofSeq [ 10; 20; 30; 20 ] | ||
| Expect.equal "findIndex" 1 (PersistentVector.findIndex (fun x -> x = 20) v) | ||
| } |
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.
🤖 This PR was created by Repo Assist, an automated AI assistant.
Summary
Adds 14 new module-level functions to
PersistentVector, continuing the work from #246 (DList) and further addressing #152 (aligning collection module functions withFSharp.Collections).PersistentVector.choosePersistentVector.existsPersistentVector.filterPersistentVector.findKeyNotFoundExceptionif none)PersistentVector.tryFindoptionPersistentVector.findIndexKeyNotFoundExceptionif none)PersistentVector.tryFindIndexoptionPersistentVector.forallPersistentVector.headArgumentExceptionif empty)PersistentVector.tryHeadoptionPersistentVector.iterPersistentVector.iteriPersistentVector.ofListPersistentVector.toArrayPersistentVector.toListfoldBack)Implementation Notes
filter,choose,ofList, andtoArrayuseTransientVectorfor efficient mutable construction before returning an immutable result — same pattern asmapandmapi.existsandforalluse awhileloop over the indexed accessor so they short-circuit on the first match/mismatch without iterating the full vector.findandfindIndexdelegate totryFindIndexand index into the vector;tryFindandtryFindIndexalso short-circuit.toListusesfoldBackto build the list in a single right-to-left pass.headandtryHeadareinlineand delegate to the indexed accessor (vector.[0]).Tests
Adds 26 new unit tests covering normal cases, empty-vector edge cases, and error-path assertions for all 14 functions.
Test Status
All tests pass. Code formatted with Fantomas before commit.
Closes #152 (partially — further collection types remain)