Skip to content
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

feat: circular slice selection #407

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

victorguarana
Copy link

Adds two functions to select item by index of a slice as if it were a circular linked list.
The difference between them is in the way they treat an empty slice, CircularSelection returns an empty instance while CircularSelect panics.
I belive CircularSelection is better, feel free to comment too.

@samber
Copy link
Owner

samber commented Jun 28, 2024

Instead of adding a new helper, I would update the lo.Nth helper, to add your % operation.

However, for selecting a range of items, I would not change lo.Slice. Instead, I would create a new helper named lo.Circular or lo.Ring, that extracts a window with offset reset.

WDYT ?

@victorguarana
Copy link
Author

victorguarana commented Jun 28, 2024

But updating the lo.Nth operation with % would change its behavior. Because it would no longer return an error when the index is greater than the collection length.

Taking advantage of your idea, do you think it would be a good idea to rename the method to lo.Circular and move it to the find.go file?

@victorguarana
Copy link
Author

@samber Changes ready. Can you check if it's ok now?

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.

2 participants