Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions iter/find_go123.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//go:build go1.23

package iter

// IndexOf returns the index at which the first occurrence of a value is found in a sequence or return -1
// if the value cannot be found.
func IndexOf[T comparable](delegate func(func(T) bool), element T) int {
index := 0

for item := range delegate {
if item == element {
return index
}

index++
}

return -1
}
29 changes: 29 additions & 0 deletions iter/find_go123_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//go:build go1.23

package iter_test

import (
"fmt"
"slices"
"testing"

li "github.com/samber/lo/iter"
"github.com/stretchr/testify/assert"
)

func ExampleIndexOf() {
numbers := slices.Values([]int{0, 1, 2, 1, 2, 3})
fmt.Println(li.IndexOf(numbers, 1))
// Output: 1
}

func TestIndexOf(t *testing.T) {
t.Parallel()
is := assert.New(t)

result1 := li.IndexOf(slices.Values([]int{0, 1, 2, 1, 2, 3}), 2)
result2 := li.IndexOf(slices.Values([]int{0, 1, 2, 1, 2, 3}), 6)

is.Equal(result1, 2)
is.Equal(result2, -1)
}
1 change: 1 addition & 0 deletions iter/iter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package iter