Skip to content

[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
repo-assist/improve-persistent-vector-funcs-152-39a775af2615965c
Open

[Repo Assist] feat: add filter, iter, exists, forall, find, choose, head, toArray, toList, ofList to PersistentVector#247
github-actions[bot] wants to merge 2 commits intomasterfrom
repo-assist/improve-persistent-vector-funcs-152-39a775af2615965c

Conversation

@github-actions
Copy link
Contributor

🤖 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 with FSharp.Collections).

Function Description
PersistentVector.choose Filter and map in a single pass
PersistentVector.exists Short-circuiting check: does any element satisfy the predicate?
PersistentVector.filter Retain only elements satisfying a predicate
PersistentVector.find First matching element (raises KeyNotFoundException if none)
PersistentVector.tryFind First matching element as option
PersistentVector.findIndex Index of first match (raises KeyNotFoundException if none)
PersistentVector.tryFindIndex Index of first match as option
PersistentVector.forall Short-circuiting check: do all elements satisfy the predicate?
PersistentVector.head First element (raises ArgumentException if empty)
PersistentVector.tryHead First element as option
PersistentVector.iter Apply an action to each element in order
PersistentVector.iteri Apply an action with element index to each element in order
PersistentVector.ofList Create a vector from a list
PersistentVector.toArray Convert to an array
PersistentVector.toList Convert to a list (via foldBack)

Implementation Notes

  • filter, choose, ofList, and toArray use TransientVector for efficient mutable construction before returning an immutable result — same pattern as map and mapi.
  • exists and forall use a while loop over the indexed accessor so they short-circuit on the first match/mismatch without iterating the full vector.
  • find and findIndex delegate to tryFindIndex and index into the vector; tryFind and tryFindIndex also short-circuit.
  • toList uses foldBack to build the list in a single right-to-left pass.
  • head and tryHead are inline and 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

Passed!  - Failed: 0, Passed: 735, Skipped: 6, Total: 741

All tests pass. Code formatted with Fantomas before commit.

Closes #152 (partially — further collection types remain)

Generated by Repo Assist ·

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@346204513ecfa08b81566450d7d599556807389f

…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>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 PersistentVector public signatures in the .fsi to 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)
}
@dsyme dsyme marked this pull request as ready for review March 16, 2026 13:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Align Collection Module functions with FSharp.Collections

2 participants