Skip to content

#13 - RemoveMatching methods return the number of removed items#31

Merged
maciej-sz merged 2 commits intomainfrom
feat/#13/remove-matching-return-count
Mar 17, 2025
Merged

#13 - RemoveMatching methods return the number of removed items#31
maciej-sz merged 2 commits intomainfrom
feat/#13/remove-matching-return-count

Conversation

@maciej-sz
Copy link
Copy Markdown
Contributor

@maciej-sz maciej-sz commented Mar 17, 2025

Summary by CodeRabbit

  • New Features

    • Removal operations across key components now return the count of items removed, providing clear feedback on actions performed.
  • Tests

    • Enhanced test cases with new fields to validate the expected removal counts, ensuring improved reliability and measurable outcomes.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 17, 2025

Walkthrough

The changes update several components to improve the reporting of the number of items removed by the RemoveMatching functionality. Method signatures across multiple files are modified to return an integer count of removed elements. Additionally, helper functions and corresponding test cases have been enhanced by adding new fields that capture the expected removal count. These updates affect interfaces, concrete map and sequence implementations, and associated tests.

Changes

File(s) Change Summary
definitions.go, functions_internal.go, map.go, mapcmp.go, sequence.go, sequencecmp.go Updated RemoveMatching methods (and helper function sliceRemoveMatching) to return an int count representing the number of removed elements, with internal logic modified to track and return this count.
common_test.go, map_cases_test.go, mutable_cases_test.go Added new expected count fields (want5 in testCase and baseMapTestCase, want3 in mutableIntTestCase) and updated test cases (including a new scenario) to validate the removal count.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant Container as RemoveMatching
    Caller->>Container: Call RemoveMatching(predicate)
    Container->>Container: Iterate through collection
    alt Element matches predicate
        Container->>Container: Increment removal count & remove element
    else 
        Container->>Container: Retain element
    end
    Container->>Caller: Return (modified container, removal count)
Loading

Poem

I'm a hopping rabbit in code so bright,
Counting each removed item with delight.
New fields and methods, updated on the fly,
Removing with precision as time ticks by.
With a twitch of my whiskers, I celebrate the bug-free night!
🥕🐇

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 golangci-lint (1.62.2)

Error: can't load config: the Go language version (go1.23) used to build golangci-lint is lower than the targeted Go version (1.24)
Failed executing command with error: can't load config: the Go language version (go1.23) used to build golangci-lint is lower than the targeted Go version (1.24)

Tip

⚡🧪 Multi-step agentic review comment chat (experimental)
  • We're introducing multi-step agentic chat in review comments. This experimental feature enhances review discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments.
    - To enable this feature, set early_access to true under in the settings.

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c19f0f5 and 27ef541.

📒 Files selected for processing (1)
  • map_cases_test.go (10 hunks)
🔇 Additional comments (1)
map_cases_test.go (1)

1452-1532: Great implementation of the removal count feature!

The added want5 field correctly captures the expected number of items removed in each test case scenario. The test cases thoroughly cover:

  • Empty collections (0 items removed)
  • One-item collections (both found and not found cases)
  • Three-item collections with different removal patterns
  • The new test case for removing odd values (where 2 items should be removed)

The test function now properly checks the return value from RemoveMatching() against the expected count.

Also applies to: 1541-1541, 1560-1562

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 17, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Files with missing lines Coverage Δ
definitions.go 100.00% <ø> (ø)
functions_internal.go 100.00% <100.00%> (ø)
map.go 100.00% <100.00%> (ø)
mapcmp.go 100.00% <100.00%> (ø)
sequence.go 100.00% <100.00%> (ø)
sequencecmp.go 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🔭 Outside diff range comments (1)
map_cases_test.go (1)

1537-1563: ⚠️ Potential issue

Missing validation of returned count in test function.

The test function doesn't capture or validate the count returned by RemoveMatching against the expected want5 value in each test case.

Apply this diff to fix the issue:

func testMapRemoveMatching(t *testing.T, builder baseMapCollIntBuilder) {
	cases := getMapRemoveMatchingCases(builder)
	for _, tt := range cases {
		t.Run(tt.name, func(t *testing.T) {
-			tt.coll.RemoveMatching(tt.args.predicate)
+			count := tt.coll.RemoveMatching(tt.args.predicate)

			actualSlice := builder.extractUnderlyingSlice(tt.coll)
			actualMap := builder.extractUnderlyingMap(tt.coll)
			actualKP := builder.extractUnderlyingKp(tt.coll)
			actualVC := builder.extractUnderlyingValsCount(tt.coll)

			if !reflect.DeepEqual(actualSlice, tt.want1) {
				t.Errorf("RemoveMatching() did not remove correctly from slice")
			}
			if !reflect.DeepEqual(actualMap, i2iToPairs(tt.want2)) {
				t.Errorf("RemoveMatching() did not remove correctly from map")
			}
			if !reflect.DeepEqual(actualKP, tt.want3) {
				t.Errorf("RemoveMatching() did not remove correctly from kp")
			}
			if actualVC != nil {
				if !reflect.DeepEqual(actualVC, tt.want4) {
					t.Errorf("RemoveMatching() did not remove correctly from values counter")
				}
			}
+			if count != tt.want5 {
+				t.Errorf("RemoveMatching() returned wrong count: %v, but wanted = %v", count, tt.want5)
+			}
		})
	}
}
🧹 Nitpick comments (1)
mapcmp.go (1)

193-217: Implementation correctly tracks and returns removed item count.

The changes properly implement the requirement to return the count of removed items. The count variable is incremented when an element matches the predicate and is correctly returned at the end of the method.

However, I notice that the RemoveValues method on lines 219-223 calls RemoveMatching but doesn't capture the returned count. Consider updating this method to also return the count for consistency.

-func (c *comfyCmpMap[K, V]) RemoveValues(v V) {
-	c.RemoveMatching(func(pair Pair[K, V]) bool {
-		return pair.Val() == v
-	})
+func (c *comfyCmpMap[K, V]) RemoveValues(v V) int {
+	return c.RemoveMatching(func(pair Pair[K, V]) bool {
+		return pair.Val() == v
+	})
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 717d538 and c19f0f5.

📒 Files selected for processing (9)
  • common_test.go (1 hunks)
  • definitions.go (1 hunks)
  • functions_internal.go (1 hunks)
  • map.go (2 hunks)
  • map_cases_test.go (8 hunks)
  • mapcmp.go (2 hunks)
  • mutable_cases_test.go (3 hunks)
  • sequence.go (1 hunks)
  • sequencecmp.go (1 hunks)
🔇 Additional comments (9)
common_test.go (1)

37-37: Adding field to support the RemoveMatching count return value

The addition of the want5 field to the testCase struct provides a place to store the expected count of removed items for testing the updated RemoveMatching functionality. This aligns with the PR objective to return the number of removed items.

definitions.go (1)

92-92: Enhancement to RemoveMatching interface

Good change - updating the RemoveMatching method in the Mutable interface to return the count of removed elements provides valuable information to callers. This enhancement is consistent with the PR objective (#13) and improves the API usability.

sequence.go (1)

96-99: Implementation of RemoveMatching with count return

The updated implementation correctly leverages the sliceRemoveMatching helper function to handle both updating the sequence and returning the count of removed elements. The implementation is clean and straightforward.

sequencecmp.go (1)

143-157: Implementation of RemoveMatching with count for CmpSequence

The implementation correctly tracks and returns the count of removed elements while maintaining the value counter. This approach differs slightly from the regular sequence implementation but is appropriate given the need to update the value counter.

functions_internal.go (1)

61-77: LGTM! The function now returns the count of removed items.

The changes appropriately modify the function signature to return both the filtered slice and a count of removed items. The implementation correctly increments the counter each time an element is removed and returns this count alongside the filtered slice.

mutable_cases_test.go (2)

106-164: LGTM! Test cases properly updated with expected removal counts.

The test cases have been correctly updated with a new want3 field that tracks the expected number of items removed by the RemoveMatching operation. The counts are accurate for each test case:

  • 0 for empty collections or when no items match
  • Proper counts for partial and full matches

167-185: LGTM! Test function now validates the returned count.

The test function has been properly updated to capture the return value from RemoveMatching and verify it against the expected count. This completes the testing of the new functionality.

map.go (1)

151-172: LGTM! The method now returns the count of removed items.

The implementation has been updated to track and return the count of elements that match the predicate. The method correctly increments the counter when an element is removed and returns the final count.

map_cases_test.go (1)

1442-1533: LGTM! Test cases properly updated with expected removal counts.

The test cases have been correctly updated with a new want5 field that tracks the expected number of items removed by the RemoveMatching operation. The added test case for finding and removing items with odd values is a good addition to the test coverage.

@maciej-sz maciej-sz merged commit 0158754 into main Mar 17, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant