roger peppe
unread,Oct 16, 2018, 3:51:34 AM10/16/18Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to kortschak, Bakul Shah, andrey mirtchovski, 1955...@gmail.com, cbeho...@gmail.com, golang-nuts
I agree that rand.Shuffle could have used an interface type that's a
subset of sort.Interface. I'm not sure why it didn't, although of
course it's easy to go from one to the other:
func main() {
x := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
xi := sort.IntSlice(x)
rand.Shuffle(xi.Len(), xi.Swap)
fmt.Println(x)
}
As for passing the slice directly, I think it's better to keep APIs
type-safe, at least to start with.
If we find ourselves writing out the swap function many times, then we
could add a rand.ShuffleSlice function along the same lines as
sort.Slice:
func ShuffleSlice(x interface{}) {
rand.Shuffle(reflect.ValueOf(x).Len(), reflect.Swapper(x))
}
As others have pointed out, this is strictly less powerful than the
current rand.Shuffle. For example, you can't shuffle two slices at
once (not that uncommon if you're storing data column-oriented for
cache friendliness).
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
golang-nuts...@googlegroups.com.