Skip to content

Conversation

bkrukowski
Copy link
Contributor

@bkrukowski bkrukowski commented Oct 30, 2024

s := slice.FromRaw([]int{1, 2, 3, 4, 5})
s.Delete(-1 /* index */, 1 /* length */)
fmt.Println(s) // [1 2 3 4]

Summary by Sourcery

Introduce support for negative indices in slice operations, allowing users to specify positions from the end of the slice. Refactor index handling by adding a solveIndex method to streamline the process. Update tests to cover the new functionality.

New Features:

  • Add support for negative indices in slice operations such as Delete, Insert, Replace, and Get.

Enhancements:

  • Refactor index handling in slice operations by introducing a solveIndex method to centralize the logic for handling negative indices.

Tests:

  • Add new test cases to verify the correct behavior of slice operations when using negative indices.

```
s := slice.FromRaw([]int{1, 2, 3, 4, 5})
s.Delete(-1 /* index */, 1 /* length */)
fmt.Println(s) // [1 2 3 4]
```
Copy link

sourcery-ai bot commented Oct 30, 2024

Reviewer's Guide by Sourcery

This PR adds support for negative indices in slice operations, allowing users to access elements from the end of the slice. The implementation extracts the index resolution logic into a new helper method solveIndex and updates all relevant slice operations to use it.

Class diagram for updated Slice operations with solveIndex

classDiagram
    class Slice {
        - T[] data
        + bool Delete(int index, int length)
        + bool Insert(int index, T... v)
        + bool Replace(int index, T... v)
        + T Get(int index)
        + void Filter(func(int, T) bool keep)
        + void Shuffle(func(int) int randIntN)
        + int solveIndex(int i)
    }
    note for Slice "solveIndex is a new helper method to handle negative indices."
Loading

File-Level Changes

Change Details Files
Added support for negative indices in slice operations
  • Created new helper method solveIndex to handle negative index resolution
  • Updated Delete, Insert, and Replace methods to support negative indices
  • Get method now uses the common solveIndex helper
slice.go
Added new example tests for negative index operations
  • Added example test for deleting the last element using negative index
  • Added example test for replacing the last element using negative index
example_test.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @bkrukowski - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding documentation about negative index support, including examples showing how negative indices work (e.g., -1 refers to last element)
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

```
s := slice.FromRaw([]int{1, 2, 3, 4, 5})
s.Delete(-1 /* index */, 1 /* length */)
fmt.Println(s) // [1 2 3 4]
```
@bkrukowski bkrukowski merged commit 9809915 into main Oct 30, 2024
8 checks passed
@bkrukowski bkrukowski deleted the negative_index branch October 30, 2024 03:04
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