-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This includes the IndexOf function and IndexOfT, as well as a small example of how to use the function to update items in the array.
- Loading branch information
1 parent
9f6960b
commit 9bb035d
Showing
3 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package linq | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestIndexOf(t *testing.T) { | ||
tests := []struct { | ||
input interface{} | ||
predicate func(interface{}) bool | ||
expected int | ||
}{ | ||
{ | ||
input: [9]int{1, 2, 3, 4, 5, 6, 7, 8, 9}, | ||
predicate: func(i interface{}) bool { | ||
return i.(int) == 3 | ||
}, | ||
expected: 2, | ||
}, | ||
{ | ||
input: "sstr", | ||
predicate: func(i interface{}) bool { | ||
return i.(rune) == 'r' | ||
}, | ||
expected: 3, | ||
}, | ||
{ | ||
input: "gadsgsadgsda", | ||
predicate: func(i interface{}) bool { | ||
return i.(rune) == 'z' | ||
}, | ||
expected: -1, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
index := From(test.input).IndexOf(test.predicate) | ||
if index != test.expected { | ||
t.Errorf("From(%v).IndexOf() expected %v received %v", test.input, test.expected, index) | ||
} | ||
|
||
index = From(test.input).IndexOfT(test.predicate) | ||
if index != test.expected { | ||
t.Errorf("From(%v).IndexOfT() expected %v received %v", test.input, test.expected, index) | ||
} | ||
} | ||
} | ||
|
||
func TestIndexOfT_PanicWhenPredicateFnIsInvalid(t *testing.T) { | ||
mustPanicWithError(t, "IndexOfT: parameter [predicateFn] has a invalid function signature. Expected: 'func(T)bool', actual: 'func(int)int'", func() { | ||
From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).IndexOfT(func(item int) int { return item + 2 }) | ||
}) | ||
} |