Skip to content

proposal: slices: add LastIndex and LastIndexFunc #63128

@dsnet

Description

@dsnet

I propose the addition of:

// LastIndex returns the index of the last occurrence of v in s, or -1 if not present.
func LastIndex[S ~[]E, E comparable](s S, v E) int

// LastIndexFunc returns the last index i satisfying f(s[i]), or -1 if none do.
func LastIndexFunc[S ~[]E, E any](s S, f func(E) bool) int

which parallels the existence of:

  • strings.IndexXXX
  • strings.LastIndexXXX
  • bytes.IndexXXX
  • bytes.LastIndexXXX

In my use-case, I'm processing Unicode runes as a []rune and wanted to find the last occurrence of a given rune.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions