-
Notifications
You must be signed in to change notification settings - Fork 79
[Repo Assist] feat: add filter, iter, exists, forall, find, choose, head, toArray, toList, ofList to PersistentVector #247
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
github-actions
wants to merge
5
commits into
master
Choose a base branch
from
repo-assist/improve-persistent-vector-funcs-152-39a775af2615965c
base: master
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
5 commits
Select commit
Hold shift + click to select a range
bd82d54
feat: add filter, iter, exists, forall, find, choose, head, toArray, …
github-actions[bot] 0fa2b33
ci: trigger checks
github-actions[bot] 7dd8592
Potential fix for pull request finding
gdziadkiewicz 9baa2eb
Merge branch 'master' into repo-assist/improve-persistent-vector-func…
gdziadkiewicz 1963130
refactor: make findIndex delegate to tryFindIndex, fix error message,…
Copilot 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -445,6 +445,165 @@ module PersistentVectorTests = | |
| (i - 1) + i * 2 |> Expect.equal "map" a.[i - 1] | ||
| } | ||
|
|
||
| test "filter should return elements satisfying predicate" { | ||
| let v = PersistentVector.ofSeq [ 1; 2; 3; 4; 5; 6 ] | ||
| let result = PersistentVector.filter (fun x -> x % 2 = 0) v | ||
| Expect.equal "filter even" [ 2; 4; 6 ] (result |> Seq.toList) | ||
| } | ||
|
|
||
| test "filter empty vector returns empty" { | ||
| let result = PersistentVector.filter (fun x -> x > 0) PersistentVector.empty<int> | ||
| Expect.equal "filter empty" 0 (result |> PersistentVector.length) | ||
| } | ||
|
|
||
| test "filter all excluded returns empty" { | ||
| let v = PersistentVector.ofSeq [ 1; 3; 5 ] | ||
| let result = PersistentVector.filter (fun x -> x % 2 = 0) v | ||
| Expect.equal "filter none" 0 (result |> PersistentVector.length) | ||
| } | ||
|
|
||
| test "iter applies function to each element" { | ||
| let v = PersistentVector.ofSeq [ 1; 2; 3 ] | ||
| let mutable sum = 0 | ||
| PersistentVector.iter (fun x -> sum <- sum + x) v | ||
| Expect.equal "iter sum" 6 sum | ||
| } | ||
|
|
||
| test "iter on empty vector does nothing" { | ||
| let mutable count = 0 | ||
| PersistentVector.iter (fun _ -> count <- count + 1) PersistentVector.empty<int> | ||
| Expect.equal "iter empty" 0 count | ||
| } | ||
|
|
||
| test "iteri passes correct indices" { | ||
| let v = PersistentVector.ofSeq [ 10; 20; 30 ] | ||
| let mutable idxSum = 0 | ||
| PersistentVector.iteri (fun i _ -> idxSum <- idxSum + i) v | ||
| Expect.equal "iteri indices" 3 idxSum // 0+1+2 | ||
| } | ||
|
|
||
| test "exists returns true when element found" { | ||
| let v = PersistentVector.ofSeq [ 1; 2; 3 ] | ||
| Expect.isTrue "exists" (PersistentVector.exists (fun x -> x = 2) v) | ||
| } | ||
|
|
||
| test "exists returns false when no element found" { | ||
| let v = PersistentVector.ofSeq [ 1; 2; 3 ] | ||
| Expect.isFalse "exists none" (PersistentVector.exists (fun x -> x = 9) v) | ||
| } | ||
|
|
||
| test "exists on empty returns false" { | ||
| Expect.isFalse "exists empty" (PersistentVector.exists (fun _ -> true) PersistentVector.empty<int>) | ||
| } | ||
|
|
||
| test "forall returns true when all elements match" { | ||
| let v = PersistentVector.ofSeq [ 2; 4; 6 ] | ||
| Expect.isTrue "forall even" (PersistentVector.forall (fun x -> x % 2 = 0) v) | ||
| } | ||
|
|
||
| test "forall returns false when some element does not match" { | ||
| let v = PersistentVector.ofSeq [ 2; 3; 6 ] | ||
| Expect.isFalse "forall not all even" (PersistentVector.forall (fun x -> x % 2 = 0) v) | ||
| } | ||
|
|
||
| test "forall on empty returns true" { | ||
| Expect.isTrue "forall empty" (PersistentVector.forall (fun _ -> false) PersistentVector.empty<int>) | ||
| } | ||
|
|
||
| test "find returns first matching element" { | ||
| let v = PersistentVector.ofSeq [ 1; 2; 3; 4 ] | ||
| Expect.equal "find" 3 (PersistentVector.find (fun x -> x > 2) v) | ||
| } | ||
|
|
||
| test "find throws when not found" { | ||
| let v = PersistentVector.ofSeq [ 1; 2; 3 ] | ||
| Expect.throws "find throws" (fun () -> PersistentVector.find (fun x -> x > 9) v |> ignore) | ||
| } | ||
|
|
||
| test "tryFind returns Some for matching element" { | ||
| let v = PersistentVector.ofSeq [ 1; 2; 3 ] | ||
| Expect.equal "tryFind" (Some 2) (PersistentVector.tryFind (fun x -> x = 2) v) | ||
| } | ||
|
|
||
| test "tryFind returns None when not found" { | ||
| let v = PersistentVector.ofSeq [ 1; 2; 3 ] | ||
| Expect.equal "tryFind none" None (PersistentVector.tryFind (fun x -> x = 9) v) | ||
| } | ||
|
|
||
| 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) | ||
| } | ||
|
Comment on lines
+533
to
+536
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot add it |
||
|
|
||
| test "findIndex throws KeyNotFoundException when not found" { | ||
| let v = PersistentVector.ofSeq [ 1; 2; 3 ] | ||
|
|
||
| Expect.throws "findIndex not found" (fun () -> PersistentVector.findIndex (fun x -> x = 99) v |> ignore) | ||
| } | ||
|
|
||
| test "findIndex throws KeyNotFoundException on empty vector" { | ||
| Expect.throws "findIndex empty" (fun () -> | ||
| PersistentVector.findIndex (fun _ -> true) PersistentVector.empty<int> | ||
| |> ignore) | ||
| } | ||
|
|
||
| test "tryFindIndex returns Some index when found" { | ||
| let v = PersistentVector.ofSeq [ 10; 20; 30 ] | ||
| Expect.equal "tryFindIndex" (Some 2) (PersistentVector.tryFindIndex (fun x -> x = 30) v) | ||
| } | ||
|
|
||
| test "tryFindIndex returns None when not found" { | ||
| let v = PersistentVector.ofSeq [ 1; 2; 3 ] | ||
| Expect.equal "tryFindIndex none" None (PersistentVector.tryFindIndex (fun x -> x = 99) v) | ||
| } | ||
|
|
||
| test "choose returns mapped Some values" { | ||
| let v = PersistentVector.ofSeq [ 1; 2; 3; 4; 5 ] | ||
|
|
||
| let result = | ||
| PersistentVector.choose (fun x -> if x % 2 = 0 then Some(x * 10) else None) v | ||
|
|
||
| Expect.equal "choose" [ 20; 40 ] (result |> Seq.toList) | ||
| } | ||
|
|
||
| test "head returns first element" { | ||
| let v = PersistentVector.ofSeq [ 7; 8; 9 ] | ||
| Expect.equal "head" 7 (PersistentVector.head v) | ||
| } | ||
|
|
||
| test "head throws on empty" { Expect.throws "head empty" (fun () -> PersistentVector.head PersistentVector.empty<int> |> ignore) } | ||
|
|
||
| test "tryHead returns Some for non-empty" { | ||
| let v = PersistentVector.ofSeq [ 42; 43 ] | ||
| Expect.equal "tryHead" (Some 42) (PersistentVector.tryHead v) | ||
| } | ||
|
|
||
| test "tryHead returns None for empty" { Expect.equal "tryHead empty" None (PersistentVector.tryHead PersistentVector.empty<int>) } | ||
|
|
||
| test "toArray returns all elements" { | ||
| let v = PersistentVector.ofSeq [ 1; 2; 3 ] | ||
| Expect.equal "toArray" [| 1; 2; 3 |] (PersistentVector.toArray v) | ||
| } | ||
|
|
||
| test "toArray on empty returns empty array" { Expect.equal "toArray empty" [||] (PersistentVector.toArray PersistentVector.empty<int>) } | ||
|
|
||
| test "toList returns all elements as list" { | ||
| let v = PersistentVector.ofSeq [ 1; 2; 3 ] | ||
| Expect.equal "toList" [ 1; 2; 3 ] (PersistentVector.toList v) | ||
| } | ||
|
|
||
| test "ofList creates vector from list" { | ||
| let v = PersistentVector.ofList [ 5; 6; 7 ] | ||
| Expect.equal "ofList length" 3 (PersistentVector.length v) | ||
| Expect.equal "ofList nth 0" 5 (PersistentVector.nth 0 v) | ||
| Expect.equal "ofList nth 2" 7 (PersistentVector.nth 2 v) | ||
| } | ||
|
|
||
| test "ofList empty creates empty vector" { | ||
| let v = PersistentVector.ofList [] | ||
| Expect.equal "ofList empty" 0 (PersistentVector.length v) | ||
| } | ||
|
|
||
| test "vector should allow init" { | ||
| let vector = PersistentVector.init 5 (fun x -> x * 2) | ||
| let s = Seq.init 5 (fun x -> x * 2) | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.