[Repo Assist] feat: add filter, iter, exists, forall, find, choose, head, toArray, toList, ofList to PersistentVector#247
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>
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.
| 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 | ||
|
|
There was a problem hiding this comment.
Done in commit 1963130. findIndex now delegates to tryFindIndex, eliminating the duplicate search loop. The error message has also been aligned with the rest of the codebase ("An index satisfying the predicate was not found in the collection"), and two new tests were added covering the KeyNotFoundException error path (no match and empty vector).
| 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) | ||
| } |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
…s-152-39a775af2615965c
… add missing tests Agent-Logs-Url: https://github.com/fsprojects/FSharpx.Collections/sessions/f75d1158-2e65-4c67-b856-c2050cbd57e1 Co-authored-by: gdziadkiewicz <8547855+gdziadkiewicz@users.noreply.github.com>
🤖 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)