forked from ahmetb/go-linq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.go
40 lines (33 loc) · 1.03 KB
/
index.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package linq
// IndexOf searches for an element that matches the conditions defined by a specified predicate
// and returns the zero-based index of the first occurrence within the collection. This method
// returns -1 if an item that matches the conditions is not found.
func (q Query) IndexOf(predicate func(interface{}) bool) int {
index := 0
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
if predicate(item) {
return index
}
index++
}
return -1
}
// IndexOfT is the typed version of IndexOf.
//
// - predicateFn is of type "func(int,TSource)bool"
//
// NOTE: IndexOf has better performance than IndexOfT.
func (q Query) IndexOfT(predicateFn interface{}) int {
predicateGenericFunc, err := newGenericFunc(
"IndexOfT", "predicateFn", predicateFn,
simpleParamValidator(newElemTypeSlice(new(genericType)), newElemTypeSlice(new(bool))),
)
if err != nil {
panic(err)
}
predicateFunc := func(item interface{}) bool {
return predicateGenericFunc.Call(item).(bool)
}
return q.IndexOf(predicateFunc)
}