Skip to content

x/exp/slices: add Replace #55358

Closed
Closed
@DeedleFake

Description

@DeedleFake

I have on multiple occasions had a need to replace elements of a slice. This is easily accomplished by calling slices.Delete() followed by slices.Insert(), but this results in more copying than is necessary. Well, unless the compiler's capable of eliminating the extra work involved, but I have no idea if it can or not.

Regardless, my proposal's simple: Add the following function to the slices package:

func Replace[S ~[]E, E any](s S, i, j int, v ...E) S

This would function exactly like

s = slices.Delete(s, i, j)
s = slices.Inset(s, i, v...)

except that it would be a bit more efficient.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions