Skip to content

Commit

Permalink
add ForEachCondition implement
Browse files Browse the repository at this point in the history
  • Loading branch information
Sianao committed Jul 2, 2024
1 parent 57c2d6d commit 49e3d79
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ func ForEach[T any](collection []T, iteratee func(item T, index int)) {
}
}

// ForEachCondition iterates over elements of collection and invokes iteratee for each element
// collection return value decide to continue or break.
func ForEachCondition[T any](collection []T, iteratee func(item T, index int) (goon bool)) {
for i := range collection {
if !iteratee(collection[i], i) {
break
}
}
}

// Times invokes the iteratee n times, returning an array of the results of each invocation.
// The iteratee is invoked with index as argument.
// Play: https://go.dev/play/p/vgQj3Glr6lT
Expand Down

0 comments on commit 49e3d79

Please sign in to comment.