Skip to content

Commit

Permalink
support parameters
Browse files Browse the repository at this point in the history
Signed-off-by: 彭锟 <[email protected]>
  • Loading branch information
kom0055 committed May 5, 2024
1 parent 3ce1a95 commit 5be6300
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 14 deletions.
3 changes: 2 additions & 1 deletion index.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package flinx
// 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 IndexOf[T any](predicate func(T) bool) func(q Query[T]) int {
func IndexOf[T any](predicates ...func(T) bool) func(q Query[T]) int {
predicate := Predicates(predicates...)
return func(q Query[T]) int {
index := 0
next := q.Iterate()
Expand Down
18 changes: 12 additions & 6 deletions result.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
)

// All determines whether all elements of a collection satisfy a condition.
func All[T any](predicate func(T) bool) func(q Query[T]) bool {
func All[T any](predicates ...func(T) bool) func(q Query[T]) bool {
predicate := Predicates(predicates...)
return func(q Query[T]) bool {
next := q.Iterate()

Expand All @@ -32,7 +33,8 @@ func Any[T any](q Query[T]) bool {
}

// AnyWith determines whether any element of a collection satisfies a condition.
func AnyWith[T any](predicate func(T) bool) func(q Query[T]) bool {
func AnyWith[T any](predicates ...func(T) bool) func(q Query[T]) bool {
predicate := Predicates(predicates...)
return func(q Query[T]) bool {
next := q.Iterate()

Expand Down Expand Up @@ -94,7 +96,8 @@ func Count[T any](q Query[T]) (r int) {

// CountWith returns a number that represents how many elements in the specified
// collection satisfy a condition.
func CountWith[T any](predicate func(T) bool) func(q Query[T]) (r int) {
func CountWith[T any](predicates ...func(T) bool) func(q Query[T]) (r int) {
predicate := Predicates(predicates...)
return func(q Query[T]) (r int) {
next := q.Iterate()

Expand All @@ -116,7 +119,8 @@ func First[T any](q Query[T]) (T, bool) {

// FirstWith returns the first element of a collection that satisfies a
// specified condition.
func FirstWith[T any](predicate func(T) bool) func(q Query[T]) (T, bool) {
func FirstWith[T any](predicates ...func(T) bool) func(q Query[T]) (T, bool) {
predicate := Predicates(predicates...)
return func(q Query[T]) (T, bool) {
next := q.Iterate()

Expand Down Expand Up @@ -179,7 +183,8 @@ func Last[T any](q Query[T]) (r T, exist bool) {

// LastWith returns the last element of a collection that satisfies a specified
// condition.
func LastWith[T any](predicate func(T) bool) func(q Query[T]) (r T, exist bool) {
func LastWith[T any](predicates ...func(T) bool) func(q Query[T]) (r T, exist bool) {
predicate := Predicates(predicates...)
return func(q Query[T]) (r T, exist bool) {
next := q.Iterate()

Expand Down Expand Up @@ -290,7 +295,8 @@ func Single[T any](q Query[T]) (r T, found bool) {

// SingleWith returns the only element of a collection that satisfies a
// specified condition, and nil if more than one such element exists.
func SingleWith[T any](predicate func(T) bool) func(q Query[T]) (r T, found bool) {
func SingleWith[T any](predicates ...func(T) bool) func(q Query[T]) (r T, found bool) {
predicate := Predicates(predicates...)
return func(q Query[T]) (r T, found bool) {
next := q.Iterate()

Expand Down
7 changes: 4 additions & 3 deletions skip.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func Skip[T any](q Query[T], count int) Query[T] {
// the result is true. After the predicate function returns false for an
// element, that element and the remaining elements in source are returned and
// there are no more invocations of predicate.
func SkipWhile[T any](predicate func(T) bool) func(q Query[T]) Query[T] {

func SkipWhile[T any](predicates ...func(T) bool) func(q Query[T]) Query[T] {
predicate := Predicates(predicates...)
return func(q Query[T]) Query[T] {
return Query[T]{
Iterate: func() Iterator[T] {
Expand Down Expand Up @@ -68,7 +68,8 @@ func SkipWhile[T any](predicate func(T) bool) func(q Query[T]) Query[T] {
// the result is true. After the predicate function returns false for an
// element, that element and the remaining elements in source are returned and
// there are no more invocations of predicate.
func SkipWhileIndexed[T any](predicate func(int, T) bool) func(q Query[T]) Query[T] {
func SkipWhileIndexed[T any](predicates ...func(int, T) bool) func(q Query[T]) Query[T] {
predicate := PredicatesIndexed(predicates...)
return func(q Query[T]) Query[T] {
return Query[T]{
Iterate: func() Iterator[T] {
Expand Down
6 changes: 4 additions & 2 deletions take.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func Take[T any](q Query[T], count int) Query[T] {

// TakeWhile returns elements from a collection as long as a specified condition
// is true, and then skips the remaining elements.
func TakeWhile[T any](predicate func(T) bool) func(q Query[T]) Query[T] {
func TakeWhile[T any](predicates ...func(T) bool) func(q Query[T]) Query[T] {
predicate := Predicates(predicates...)
return func(q Query[T]) Query[T] {
return Query[T]{
Iterate: func() Iterator[T] {
Expand Down Expand Up @@ -61,7 +62,8 @@ func TakeWhile[T any](predicate func(T) bool) func(q Query[T]) Query[T] {
// function. The first argument of predicate represents the zero-based index of
// the element within collection. The second argument represents the element to
// test.
func TakeWhileIndexed[T any](predicate func(int, T) bool) func(q Query[T]) Query[T] {
func TakeWhileIndexed[T any](predicates ...func(int, T) bool) func(q Query[T]) Query[T] {
predicate := PredicatesIndexed(predicates...)
return func(q Query[T]) Query[T] {
return Query[T]{
Iterate: func() Iterator[T] {
Expand Down
24 changes: 24 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,27 @@ func ValidateQuery[T comparable](q Query[T], output []T) bool {
_, ok2 := next()
return !(ok || ok2)
}

func Predicates[T any](predicates ...func(T) bool) func(T) bool {
return func(t T) bool {
for i := range predicates {
item := predicates[i]
if !item(t) {
return false
}
}
return true
}
}

func PredicatesIndexed[T any](predicates ...func(int, T) bool) func(int, T) bool {
return func(idx int, t T) bool {
for i := range predicates {
item := predicates[i]
if !item(idx, t) {
return false
}
}
return true
}
}
6 changes: 4 additions & 2 deletions where.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package flinx

func Where[T any](predicate func(T) bool) func(q Query[T]) Query[T] {
func Where[T any](predicates ...func(T) bool) func(q Query[T]) Query[T] {
//predicateIdx := func(_ int, item T) bool {
// return predicate(item)
//}
//return WhereIndexed(predicateIdx)
predicate := Predicates(predicates...)
return func(q Query[T]) Query[T] {
return Query[T]{
Iterate: func() Iterator[T] {
Expand All @@ -28,7 +29,8 @@ func Where[T any](predicate func(T) bool) func(q Query[T]) Query[T] {
//
// The first argument represents the zero-based index of the element within
// collection. The second argument of predicate represents the element to test.
func WhereIndexed[T any](predicate func(int, T) bool) func(q Query[T]) Query[T] {
func WhereIndexed[T any](predicates ...func(int, T) bool) func(q Query[T]) Query[T] {
predicate := PredicatesIndexed(predicates...)
return func(q Query[T]) Query[T] {
return Query[T]{
Iterate: func() Iterator[T] {
Expand Down

0 comments on commit 5be6300

Please sign in to comment.