You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The signature in that version of the proposal is func Repeat[T any](count int, xs ...T) []T.
I propose a small, trivial addition to the slices package.
// Repeat returns a new slice containing count copies of x.funcRepeat[Tany](xT, countint) []T {
ifcount<0 {
panic("count must be non-negative")
}
xs:=make([]T, count)
fori:=0; i<count; i++ {
xs[i] =x
}
returnxs
}
There's prior, related art in other languages with list-like types, including the following. (The Python and JavaScript examples were provided by @Jorropo and @earthboundkid; see below.) Not all of the below are identical in their behaviour, but they're thematically similar.
Proposal Details
Note
The current version of this proposal is here.
The signature in that version of the proposal is
func Repeat[T any](count int, xs ...T) []T.I propose a small, trivial addition to the slices package.
There's prior, related art in other languages with list-like types, including the following. (The Python and JavaScript examples were provided by @Jorropo and @earthboundkid; see below.) Not all of the below are identical in their behaviour, but they're thematically similar.
*for sequences.I've personally found it a useful helper to have—particularly when combined with Concat, which looks like it'll make its way into Go 1.22.
I appreciate that slices oughtn't become a dumping ground for everyone's ideas about what's useful, but this feels like a natural addition to me.
I'm happy to contribute a pull request with tests if this is considered a good idea.