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

proposal: slices: add Trim, TrimRight, and TrimLeft #66672

Open
dsnet opened this issue Apr 4, 2024 · 6 comments
Open

proposal: slices: add Trim, TrimRight, and TrimLeft #66672

dsnet opened this issue Apr 4, 2024 · 6 comments
Labels
Milestone

Comments

@dsnet
Copy link
Member

dsnet commented Apr 4, 2024

Proposal Details

I propose the addition of:

// Trim returns a slice with all leading and trailing elements of s contained in v removed.
func Trim[S ~[]E, E comparable](s S, v ...E) S

// TrimLeft returns a slice with all leading elements of s contained in v removed.
func TrimLeft[S ~[]E, E comparable](s S, v ...E) S

// TrimRight returns a slice with all trailing elements of s contained in v removed.
func TrimRight[S ~[]E, E comparable](s S, v ...E) S

// TrimFunc returns a slice with all leading and trailing elements of s that satisfy f removed.
func TrimFunc[S ~[]E, E any](s S, f func(E) bool) bool

// TrimLeftFunc returns a slice with all leading elements of s that satisfy f removed.
func TrimLeftFunc[S ~[]E, E any](s S, f func(E) bool) bool

// TrimRightFunc returns a slice with all trailing elements of s that satisfy f removed.
func TrimRightFunc[S ~[]E, E any](s S, f func(E) bool) bool

These functions match similar functions found in the "bytes" and "strings" packages:

  • Trim
  • TrimLeft
  • TrimRight
  • TrimFunc
  • TrimLeftFunc
  • TrimRightFunc
@dsnet dsnet added the Proposal label Apr 4, 2024
@gopherbot gopherbot added this to the Proposal milestone Apr 4, 2024
@ianlancetaylor
Copy link
Contributor

While in general we want slices to follow bytes and strings, I think we've learned over the years that the use of "left" and "right" is parochial and confusing. We should use "begin" and "end" or something similar.

@bjorndm
Copy link

bjorndm commented Apr 4, 2024

While we are bike shedding, I feel the comments on the API suggest TrimLeading and TrimTrailing.

@seankhliao
Copy link
Member

the *Func variants feel really close to a theoretical Filter

@dsnet
Copy link
Member Author

dsnet commented Apr 4, 2024

Changing the words "Left" and "Right" sounds fine to me, but I do have a preference for consistency with the "strings" and "bytes" package. Presumably we would introduce functions with the updated names there too?

@xiaokentrl

This comment was marked as duplicate.

@earthboundkid
Copy link
Contributor

the *Func variants feel really close to a theoretical Filter

It's actually more like DropWhile because it stops filtering after the first false return and then just returns the rest, even if it passes the filter.

I like slices.DeleteFunc because it takes the convoluted idiom for filtering a slice in place and makes it much simpler. If these return new slices, I'm not sure they meet the bar for simplifying complicated code you could just write some other way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

7 participants